Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get screen resolution in Android Honeycomb?

I want to get real resolution of screen on Android Honeycomb.

Here's my code

Display display = getWindowManager().getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();

My device is Asus Transformer TF101 with size are 1280x800.

But above code make w = 1280 and h = 752 (That i want is 800 not 752).

I know h < 800 because it's subtracted for status bar.

Have any way to get real height of screen?

Many thanks!

like image 232
Levanphong7887 Avatar asked Aug 27 '11 09:08

Levanphong7887


People also ask

How do you change screen resolution on Android?

1 Go to the Settings menu > Display. 2 Tap on Screen resolution. 3 Select a resolution by sliding the circle. Tap on Apply once you have selected your preferred screen resolution.


1 Answers

Starting Andorid 3.2, the height of system status bar is not included in DisplayMetrics's height, you have to use undocumented APIs (Display.getRawWidth() and Display.getRawHeight()) to get the physical screen width or height.

Here is the example to show you how to get the physical screen width or height.

Method mGetRawW = Display.class.getMethod("getRawWidth");
Method mGetRawH = Display.class.getMethod("getRawHeight");
int nW = (Integer)mGetRawW.invoke(dp);
int nH = (Integer)mGetRawH.invoke(dp);

UPDATED: For API 13-16, you have to use the above code to get real width/height. For API 17+, you can now use the new public API, Display.getRealSize().

like image 72
Sam Lu Avatar answered Oct 21 '22 06:10

Sam Lu