import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm=getFragmentManager();
FragmentTransaction ft= fm.beginTransaction();
WindowManager wm= getWindowManager();
Display d=wm.getDefaultDisplay();
if (d.getWidth()>d.getHeight())
{
Fragment1 f1 = new Fragment1();
ft.replace(android.R.id.content, f1);
}
else
{
Fragment2 f2 = new Fragment2();
ft.replace(android.R.id.content, f2);
}
ft.commit();
}
}
I am trying to use fragments in android and want to display fragment1
when width of display is greater than height of display and fragment2
when height of display is greater than width of display but when using getWidth()
and getHeight()
android studio is saying that these methods are depricated.
Then how to know when width is greater and when height is greater?
Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size. x; int height = size.
Step 1 − Create a new project in Android Studio,go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above example we have create a dynamic textview and added textview properties like setText(),setPadding(),SetGravity().
You can use the following method to get the width and height of the view, For example, int height = yourView. getLayoutParams(). height; int width = yourView.
Use DisplayMetrics
instead of Display
.
Try this:
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
import android.util.DisplayMetrics;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm=getFragmentManager();
FragmentTransaction ft= fm.beginTransaction();
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
if (width > height)
{
Fragment1 f1 = new Fragment1();
ft.replace(android.R.id.content, f1);
}
else
{
Fragment2 f2 = new Fragment2();
ft.replace(android.R.id.content, f2);
}
ft.commit();
}
}
You can use DisplayMetrics
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int Width = displaymetrics.widthPixels;
if (Width > height )
{
Fragment1 f1 = new Fragment1();
ft.replace(android.R.id.content, f1);
}
else
{
Fragment2 f2 = new Fragment2();
ft.replace(android.R.id.content, f2);
}
DisplayMetrics displaymetrics = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;
Try
context.getWindowManager().getDefaultDisplay().getMetrics(dm);
if(dm.widthPixels > dm.heightPixels) {
//width is greater, i.e. landscape
Fragment1 f1 = new Fragment1();
ft.replace(android.R.id.content, f1);
} else {
//height is greater i.e. portrait
Fragment2 f2 = new Fragment2();
ft.replace(android.R.id.content, f2);
}
You can detect orientation directly by overriding onConfigurationChanges
in MainActivity
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Fragment1 f1 = new Fragment1();
ft.replace(android.R.id.content, f1);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Fragment2 f2 = new Fragment2();
ft.replace(android.R.id.content, f2);
}
}
//Add configChanges in manifest
<activity android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
Use this to get width and height.
int Measuredwidth = 0;
int Measuredheight = 0;
Point size = new Point();
WindowManager wm = getWindowManager();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
wm.getDefaultDisplay().getSize(size);
Measuredwidth = size.x;
Measuredheight = size.y;
}else{
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
Measuredheight = d.getHeight();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With