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:
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.
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.
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.
Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size. x; int height = size.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With