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?
Asked
Active
Viewed 6.4k times
3 Answers
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);

evilone
- 22,410
- 7
- 80
- 107
-
So what if i wanted to go by the inch? – yoshi24 Jul 05 '11 at 21:47
-
2This is wrong !!! , this is not calculation of px it is calculation of dip. http://stackoverflow.com/questions/6840904/how-to-get-the-screen-size-of-the-device/6841335#6841335 – Lukap Jul 27 '11 at 08:21
-
1Take devices see their specifications and you will see that with this code you get wrong results ! – Lukap Jul 27 '11 at 08:22
-
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... – Display Name Mar 24 '13 at 00:50
-
@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. – Rev Tyler May 14 '13 at 04:36
-
@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? – Display Name May 14 '13 at 09:14
-
1@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. – Rev Tyler May 14 '13 at 17:58
-
@Lukap thank you for your link, for that's the correct answer. Above code drove me crazy for past few days. – NightFury Feb 27 '14 at 21:17
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
}

inazaruk
- 74,247
- 24
- 188
- 156
-
Ok thanks! so how would i compare that to say? 10.2 inch screen? like what metric does it go by? – yoshi24 Jul 05 '11 at 21:42
-
-
1Thanks. 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'. – milkersarac Oct 13 '12 at 22:19
-
1
0
In your onCreate or any other Activity method simply do:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();

azharb
- 979
- 6
- 15