1

Has anyone noticed this issue and resolved the way of getting the absolute display size consistently in both orientations?

Example, Nexus 5 (5.0.1):

  • Portrait: width = 1080 height = 1776
  • Landscape: width = 1794 height = 1080

I would have thought that the height in portrait would match the width in landscape. Initially suspected the status bar, but docs are clear. Anyway the status bar height in this example is 75px in either orientation and the diff in the example is 18px.

Code snippet for display width and height in pixels:

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;

String dimensions = String.format("width = %d height = %d", width, height);
Log.v(TAG, dimensions);
dobridog
  • 380
  • 4
  • 12

2 Answers2

4

Assuming the display is FullHD acording to your logging there's something of 144px (48dp) height in portrait and 126px (42dp) width in landscape occupying the display (when scaling factor is 3 which is xxhdpi). I bet it's the navigation bar with Back, Home and Recent buttons. This is sufficient for choosing layouts.

EDIT:

If you need the full display size the following code is available from API 17:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
if (Build.VERSION.SDK_INT >= 17) {
    display.getRealSize(size);
} else {
    display.getSize(size); // correct for devices with hardware navigation buttons
}

EDIT2:

If you want to make sure you get correct result on API 14 through 16 consider following this answer along with its comments https://stackoverflow.com/a/11004877/2444099 .

Community
  • 1
  • 1
Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • [getRealSize()](http://developer.android.com/reference/android/view/Display.html#getRealSize(android.graphics.Point)) seams like the API to be used for devices with soft nav bar. `getSize()` will have the same effect as the example from the question. Good catch @Eugen – dobridog May 20 '15 at 23:43
  • Correct. But it's not available below API 17. Until API 10 there were no soft keys so `getSize()` is OK. There are virtually no API 11 through API 13 devices so lets say `getSize()` is OK as well. For API 14 through 16 you'll need to use reflection to get the real size. – Eugen Pechanec May 20 '15 at 23:49
0

It's not the entire screen size, its the size of the UI that display metrics gives you. You have to factor in the amount of screen size taken up by the area taken up by the status bar (clock, battery level, signal strength bar) which will differ on orientation.

ataulm
  • 15,195
  • 7
  • 50
  • 92
Andrew S
  • 2,847
  • 3
  • 33
  • 50
  • Andrew, I suspected the same thing, however the [measured status bar height](http://stackoverflow.com/questions/3355367/height-of-statusbar/3356263#3356263) is the same in both orientations. Sorry about the confusion with the action bar, I was referring to the status bar in my question - corrected now. – dobridog May 10 '15 at 22:43