What should I use in place of getWidth() and getHeight() to know when width is greater and when height is greater [duplicate]
Asked Answered
L

5

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?

Laundryman answered 19/4, 2017 at 6:29 Comment(4)
are you trying to show fragment one on portrait and fragment 2 on landscapeLymn
@Shararti KAKA yes....I am trying to do the same thingLaundryman
use DisplayMetrics Detta
When you get four answers in as many minutes, it's a sure sign that you did not do enough research.Plagiary
L
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();
    }
}
Laboured answered 19/4, 2017 at 6:33 Comment(5)
@Laundryman kindly read this: stackoverflow.com/help/someone-answersLaboured
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 thatLaundryman
You have already accepted this answer. Thank you :) Make sure to give an up-vote later.Laboured
already done friendLaundryman
Previously you accepted my answer but now you have changed it. May I know why?Laboured
S
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);
    }
Sulph answered 19/4, 2017 at 6:33 Comment(1)
thanks for the answer and the valuable timeLaundryman
A
2
DisplayMetrics displaymetrics = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;
Abyssinia answered 19/4, 2017 at 6:33 Comment(0)
U
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(); 
}
Uzial answered 19/4, 2017 at 6:32 Comment(2)
where is this x and y coming fromLaundryman
it is the display x and y dimensions in pixelsUzial
G
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">
Gallium answered 19/4, 2017 at 6:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.