49
DisplayMetrics metrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;

I use these codes to get the height and width of screen

the screen height on my Nexus7 should be 1280

but it return 1205...

and my minSdkVersion is level 8

so i can't use these method:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screen_width = size.x;
int screen_height = size.y;

now, how should i get the correct screen size ?

Edit:

if (Build.VERSION.SDK_INT >= 11) {
        Point size = new Point();
        try {
            this.getWindowManager().getDefaultDisplay().getRealSize(size);
            screenWidth = size.x;
            screenHeight = size.y;
        } catch (NoSuchMethodError e) {
            Log.i("error", "it can't work");
        }

    } else {
        DisplayMetrics metrics = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        screenWidth = metrics.widthPixels;
        screenHeight = metrics.heightPixels;
    }

use it works!

user1531240
  • 875
  • 1
  • 11
  • 20
  • Does this help? http://stackoverflow.com/questions/1016896/android-how-to-get-screen-dimensions – trojanfoe Jan 15 '13 at 15:56
  • No, i can't use `display.getSize(size);` since my min SDK is level 8 – user1531240 Jan 15 '13 at 16:01
  • well if you read some of the comments in trojanfoe's link they suggest something like: try { display.getSize(size); width = size.x; height = size.y; } catch (NoSuchMethodError e) { width = display.getWidth(); height = display.getHeight(); } .... however, even if you can use the getSize() method, i think you will still get this 1205 resolution which is the available screen height (i.e. minus navigation buttons). Not sure how to get height of nav buttons, but interested and looking. – ggenglish Jan 15 '13 at 16:14
  • 4
    In your edit, you check for an API level >= 11. Display#getRealSize() was added in API level 17 so you should be checking for that level instead. http://developer.android.com/reference/android/view/Display.html#getRealSize%28android.graphics.Point%29 – Takaitra Mar 18 '15 at 17:32
  • See https://stackoverflow.com/questions/63407883/getting-screen-width-on-api-level-30-android-11-getdefaultdisplay-and-getme. – CoolMind Aug 23 '21 at 11:49
  • For API level 31+, use `windowManager.currentWindowMetrics.bounds.height` – Zain May 07 '22 at 21:36

3 Answers3

53

I've written a method that will get as close as possible for API 10 through 17+. It should give the real width and height for API 14+, and it does as well as it can for everything else.

    Display display = context.getWindowManager().getDefaultDisplay();
    int realWidth;
    int realHeight;

    if (Build.VERSION.SDK_INT >= 17){
        //new pleasant way to get real metrics
        DisplayMetrics realMetrics = new DisplayMetrics();
        display.getRealMetrics(realMetrics);
        realWidth = realMetrics.widthPixels;
        realHeight = realMetrics.heightPixels;

    } else if (Build.VERSION.SDK_INT >= 14) {
        //reflection for this weird in-between time
        try {
            Method mGetRawH = Display.class.getMethod("getRawHeight");
            Method mGetRawW = Display.class.getMethod("getRawWidth");
            realWidth = (Integer) mGetRawW.invoke(display);
            realHeight = (Integer) mGetRawH.invoke(display);
        } catch (Exception e) {
            //this may not be 100% accurate, but it's all we've got
            realWidth = display.getWidth();
            realHeight = display.getHeight();
            Log.e("Display Info", "Couldn't use reflection to get the real display metrics.");
        }

    } else {
        //This should be close, as lower API devices should not have window navigation bars
        realWidth = display.getWidth();
        realHeight = display.getHeight();
    }
CorayThan
  • 17,174
  • 28
  • 113
  • 161
  • Lovely method. Wish I could upvote more. Must be the selected answer. – Sufian Jan 02 '15 at 10:11
  • 1
    Note that the context must be an activity – Chris Sprague Jan 08 '15 at 02:45
  • 2
    Oh man thank you....how on earth something this ****'ing simple can be this complex...beyond me...should be 1 line: getScreenDimensions()! Get your shit together Google. – Aggressor Jul 16 '15 at 23:09
  • Works great on real devices, on simulators it may not yield correct results, which is not really a problem. – Meanman Jul 31 '16 at 16:06
  • this approach is not working the height is not coming exact in any android devices –  Jan 19 '17 at 12:33
  • @FerozSiddiqui What SDK versions? I wrote this a couple years ago, so it's possible newer API versions require yet another else. – CorayThan Jan 19 '17 at 17:54
  • my app supports minimum level of 16 till nougat which is 24 api level –  Jan 21 '17 at 14:52
  • It is deprecated. See https://stackoverflow.com/questions/63407883/getting-screen-width-on-api-level-30-android-11-getdefaultdisplay-and-getme. – CoolMind Aug 23 '21 at 11:49
39

I think this will work. The trick is you must use:

display.getRealSize(size);

not

display.getSize(size);

To deal with your API 8 coding issue do something like this:

try { 
    display.getRealSize(size);
    height = size.y; 
} catch (NoSuchMethodError e) {
    height = display.getHeight();
}

Only more recent API devices will have onscreen navigation buttons and thus need the new method, older devices will throw an exception but will not have onscreen navigation thus the older method is fine.

In case it needs to be said: Just because you have a minimumAPI level of 8 for your project doesn't mean you have to compile it at that level. I also use level 8 for minimum but my project mostly are compiled at level 13 (3.2) giving them access to a lot of new methods.

ggenglish
  • 1,668
  • 18
  • 19
  • 2
    instead of using a try-catch block you can check the api level on the device. see Build.VERSION and methods Display.getHeight, Display.getSize, display.getRealSize – mehmet6parmak Jul 19 '13 at 05:47
  • 3
    Keep in mind that this method won't work for API level 14, 15 and 16 as the `getRealSize()` method was only introduced in API level 17. Here's a workaround to find the raw values < API 17: http://stackoverflow.com/questions/10991194/android-displaymetrics-returns-incorrect-screen-size-in-pixels-on-ics – Saket Jan 31 '14 at 19:53
5

You can find real device dimensions with either using Display or DisplayMetrics

Using Display:

private void findRealSize(Activity activity)
{
    Point size = new Point();
    Display display = activity.getWindowManager().getDefaultDisplay();

    if (Build.VERSION.SDK_INT >= 17)
        display.getRealSize(size);
    else
        display.getSize(size);

    int realWidth = size.x;
    int realHeight = size.y;

    Log.i("LOG_TAG", "realWidth: " + realWidth + " realHeight: " + realHeight);
}

Using DisplayMetrics:

private void findRealSize(Activity activity)
{
    DisplayMetrics displayMetrics = new DisplayMetrics();

    if (Build.VERSION.SDK_INT >= 17)
    {
        activity.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
    }
    else
    {
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    }

    int realWidth = displayMetrics.widthPixels;
    int realHeight = displayMetrics.heightPixels;

    Log.i("LOG_TAG", "realWidth: " + realWidth + " realHeight: " + realHeight);
}
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
  • `getDefaultDisplay` is deprecated. See https://stackoverflow.com/questions/63407883/getting-screen-width-on-api-level-30-android-11-getdefaultdisplay-and-getme. – CoolMind Aug 23 '21 at 11:46