6
votes

I have a screen size of 600 (width) x 1024 (height). I get current width 600 but incorrectly get height 976 (without rotated screen). I get current width 1024 but wrong get height 552 (with rotated screen).

 int rowPixelWidth = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
                int rowWidth =  (int)Math.floor(rowPixelWidth / this.getResources().getDisplayMetrics().density);
                int rowPixelheight = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();
                int rowheight =  (int)Math.floor(rowPixelheight / this.getResources().getDisplayMetrics().density);
                Log.d("rowWidth","rowWidth"+rowWidth);
                Log.d("rowheight","rowheight"+rowheight);
-------------------------------------------------------------
 <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="8" />
    <supports-screens android:anyDensity="true"/>
-------------------------------------------------------------

What's wrong with this code?

Here are some numbers for typical screen widths:

  1. 320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
  2. 480dp: a tweener tablet like the Streak (480x800 mdpi).
  3. 600dp: a 7” tablet (600x1024 mdpi).
  4. 720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).

I get current width but not get current height for all device (without (320x480 screen).

I try code from Get screen dimensions in pixels but same occur problem.

2
Your code is correct. Some devices subtract the screen area for the former hardware keys, which are now touch buttons on the display, from the available screen resolution.class stacker
i get current width and height only 320(width)x480(height) screen but not get current width and height another screen.AnilPatel
And why is this a problem?class stacker
my true all button margin setting in 320x480 but not get this setting another screen.AnilPatel

2 Answers

9
votes

I believe the getDefaultDisplay only gives you the window where the app is going to be displayed, it does not account for other things like notification bar on top or android buttons (home, back...) at the bottom or things like that.

That's probably why you see a difference of 48 pixels in the vertical size in both cases.

MORE INFO

Your code seems to be right and similar to what's there. You might also want to use something like in this answer to use the right API for the right version of Android.

But in any case, the size you will get will not include the navigation bar with the soft Android buttons when they are present.

When you run Android 4.1 on a HVGA screen, there is no navigation bar or combined bar (screen is too small for that), so you get the resolution for the whole screen.

If you run the same version on a larger screen, you will see the navigation bar, the display size available for your app is smaller and you will only get the resolution for that space.

Please check the definitions of navigation bar and others here if you are not clear.

0
votes

Your screen height with above code doesn't includes the android buttons (home, back, recent apps) at the bottom with a width of 48px. As a result of which you are getting screen height as 1024-48 = 976.

Above function gives the area in which app can be seen. It is possible to make your app as full screen app, which will hide the notification bar at top, but it still wont hide android buttons bar (home, back, recent apps) at the bottom. So the maximum screen space you can utilize is 1024-48=976 px.

However in some phones android buttons are not a part of screen, in those cases you can use whole 1024 px of screen.