Android: How to get the real screen size of the device?

Trois picture Trois · Nov 29, 2012 · Viewed 13.6k times · Source

I have tried different ways to get the screen size of the device, but it always returned me the wrong size (791x480 instead of 854x480). It might be due to the navigation bar. My device is currently running JellyBean 4.1.1.

I tried:

Display display = getWindowManager().getDefaultDisplay();  
Point size = new Point();
display.getSize(size);
OpenGLRenderer.widthScreen = size.x;
OpenGLRenderer.heightScreen = size.y;

And:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
String resolution = dm.widthPixels + "x" + dm.heightPixels;

I'm using the following code to make the app full screen:

setContentView(mGLSurfaceView);
mGLSurfaceView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

I noticed that in the logcat, the right size is returned:

01-01 03:05:06.967: I/InputReader(1961): 
    Device reconfigured: id=8, name='cyttsp-spi', 
    surface size is now 480x854, mode is 1

In my AndroidManifest:

uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"

I used:

Why am I not able to get the real size (480x854)? Or how can I use the surface returned by the “Device reconfigured”?

Answer

Andrew Campbell picture Andrew Campbell · Nov 29, 2012

Try calling

    display.getRealSize();

DISCLAIMER: I did not realize this, but this will only work for API 17 (Android 4.2/JellyBean) and up.