Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 370
Ankit Avatar asked Apr 19 '17 06:04

Ankit


People also ask

How can you get the screen width and height and use this value in your app?

Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size. x; int height = size.

How can I get height and width of a view in android programmatically?

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().

How do I get custom height view?

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.


5 Answers

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();
    }
}
like image 106
Ferdous Ahamed Avatar answered Oct 19 '22 14:10

Ferdous Ahamed


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);
    }
like image 37
Kishore Jethava Avatar answered Oct 19 '22 14:10

Kishore Jethava


DisplayMetrics displaymetrics = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;
like image 42
DroidDev Avatar answered Oct 19 '22 15:10

DroidDev


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">
like image 32
PEHLAJ Avatar answered Oct 19 '22 13:10

PEHLAJ


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(); 
}
like image 30
Fathima km Avatar answered Oct 19 '22 15:10

Fathima km