11

Devices with Ice Cream Sandwich can use on-screen soft keys, and this means that some of the screen estate is taken by these keys. I need to get the real screen resolution, and not the resolution minus the space taken up by the soft keys.

I've tried the following, but neither works:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();
Log.d("SCREEN RES", "Width: " + width + ". Height: " + height);

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
height = metrics.heightPixels;
width = metrics.widthPixels;
Log.d("SCREEN RES", "Width: " + width + ". Height: " + height);

Here's the output:

12-19 20:18:42.012: D/SCREEN RES(20752): Width: 1196. Height: 720
12-19 20:18:42.012: D/SCREEN RES(20752): Width: 1196. Height: 720

The real screen resolution is 1280 x 720 pixels, not 1196 x 720 pixels.

Any ideas?

Edit

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

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
Michell Bak
  • 13,182
  • 11
  • 64
  • 121
  • Conclusion about how to get real resolution: http://stackoverflow.com/questions/11824193/htc-sensation-real-resolution – thecr0w Aug 09 '12 at 02:09

2 Answers2

9

The API returns the number of pixels an app can use, so this is the correct result.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • What if I put the app in full screen mode like this? `getWindow().getDecorView().setSystemUiVisibility(2);`. This removes the soft keys and allows me to utilize the entire screen and all 1280 pixels of width. – Michell Bak Dec 19 '11 at 19:43
  • Can't edit my previous comment, but 2 == `View.SYSTEM_UI_FLAG_HIDE_NAVIGATION`. – Michell Bak Dec 19 '11 at 19:49
  • 9
    @Romain Guy - There are situations where the app needs to know the full size of the screen, even though it can use less than that. For example, the designer might want to have something centered on the device screen, not on the app-available space. So, while there is a need for an API that returns the number of pixels the app can use, there's also a need for an API that returns the physical screen dimensions. – Franci Penov Dec 19 '11 at 19:53
  • Conclusion about how to get real resolution: http://stackoverflow.com/questions/11824193/htc-sensation-real-resolution – thecr0w Aug 09 '12 at 02:10
  • I'm also curious about the conclusion. I have a situation where I'm calculating how much space I can use when the screen rotates. Since I can't get the real phyisical size, I can't calculate how much space the soft-keys are taking up. – sroes Feb 28 '13 at 14:22
  • See the answer I added. Pretty simple from API 17+ :) – head in the codes Aug 28 '14 at 22:32
1

From API level 17 (Android 4.2 Jelly Bean), you can get the real size of the display as follows:

Point size = new Point();
Display display = getWindowManager().getDefaultDisplay();
display.getRealSize(size);

size will now contain the display size based on the orientation (ie. x will be left/right relative to the user, not the device)

head in the codes
  • 1,159
  • 12
  • 24