How to get screen size of device? [duplicate]

I would like to get the height of a android screen and if the screen inst a certain height, how would i go about doing this?


Solution 1:

If you want the display dimensions in pixels you can use this code:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

Then you can add condition that compares the height to satisfy your needs.

In inches:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels/dm.xdpi,2);
double y = Math.pow(dm.heightPixels/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);
Log.d("debug","Screen inches : " + screenInches);

Solution 2:

From within activity:

int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();

Or if you only have Context object:

WindowManager windowManager = (WindowManager)mContext.getSystemService(WINDOW_SERVICE);
int width = windowManager.getDefaultDisplay().getWidth();
int height = windowManager.getDefaultDisplay().getHeight()

UPDATED. How to detect your application runs on large screen:

//Android Level 9 and up:
Configuration config = getResources().getConfiguration();
if((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) ==  
    Configuration.SCREENLAYOUT_SIZE_XLARGE) 
{
    //xlarge screen
}