10
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?

4
  • 1
    are you trying to show fragment one on portrait and fragment 2 on landscape Commented Apr 19, 2017 at 6:31
  • @Shararti KAKA yes....I am trying to do the same thing Commented Apr 19, 2017 at 6:32
  • use DisplayMetrics Commented Apr 19, 2017 at 6:34
  • 2
    When you get four answers in as many minutes, it's a sure sign that you did not do enough research. Commented Apr 19, 2017 at 6:34

5 Answers 5

4

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(); } } 
Sign up to request clarification or add additional context in comments.

5 Comments

sir, I was about to check the tick but the site was giving warning that I can check the tick only after 10 minutes so I was waing for that
You have already accepted this answer. Thank you :) Make sure to give an up-vote later.
already done friend
Previously you accepted my answer but now you have changed it. May I know why?
3

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); } 

1 Comment

thanks for the answer and the valuable time
2
DisplayMetrics displaymetrics = new DisplayMetrics(); mContext.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int screenWidth = displaymetrics.widthPixels; int screenHeight = displaymetrics.heightPixels; 

Comments

1

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"> 

Comments

1

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(); } 

2 Comments

where is this x and y coming from
it is the display x and y dimensions in pixels

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.