How to get screen size of device? [duplicate]
Asked Answered
P

3

42

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?

Peng answered 5/7, 2011 at 21:29 Comment(0)
B
65

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);
Briolette answered 5/7, 2011 at 21:30 Comment(8)
So what if i wanted to go by the inch?Peng
This is wrong !!! , this is not calculation of px it is calculation of dip. #6841404Tole
Take devices see their specifications and you will see that with this code you get wrong results !Tole
Funny enough: SDK says that these methods are deprecated and should be replaced by getSize(Point), but the latter causes NoSuchMethodException at least on my device. I'm wondering what's the purpose of such deprecation...Kimon
@SargeBorsch What version of Android is your device running when you get this error? getSize(Point) is only in Android 3 and up. For anything less than 3, you'll need to use the old method.Jabez
@RevTyler 2.3.4. But why then they marked method as deprecated, if it is required to support Android 2.3.4 and, possibly, some other popular devices, which cannot be easily upgraded?Kimon
@SargeBorsch Deprecated doesn't mean you should never use it. Deprecated means that you should only use it if necessary. It has been replaced, but they keep the code there for older devices that can't be upgraded. You would do an if(android.os.Build.VERSION.SDK_INT >= 13) to determine whether you should use the new code, or deprecated code.Jabez
@Tole thank you for your link, for that's the correct answer. Above code drove me crazy for past few days.Lesbos
S
22

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
}
Schwitzer answered 5/7, 2011 at 21:30 Comment(4)
Ok thanks! so how would i compare that to say? 10.2 inch screen? like what metric does it go by?Peng
Cool! Thanks man thats actually better!Peng
Thanks. But I guess there is a small bug for the 'Context' object one. The constant should be 'Context.WINDOW_SERVICE' instead of plain 'WINDOW_SERVICE'.Harar
getHeight() and getWidth() for getDefaultDisplay() are deprecated.Caudill
J
0

In your onCreate or any other Activity method simply do:

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); 
int height = display.getHeight();
Janey answered 5/7, 2011 at 21:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.