Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong display width and height in Android

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.

like image 481
AnilPatel Avatar asked Apr 22 '13 06:04

AnilPatel


People also ask

How do I make my Android app fit the screen?

Open up Settings and then tap on Display. Scroll down and tap on Display size. In this new screen, drag the slider to left to shrink the display size or the right to enlarge it.

How do I resize my Android screen?

To make your display size smaller or larger: On your device, open the Settings app. Search and select Display size. To change your preferred display size, move the slider left or right.

How can I get mobile screen width and height?

Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size. x; int height = size.


1 Answers

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.

like image 131
Matthieu Avatar answered Sep 19 '22 16:09

Matthieu