Android: Showing Wrong Screen Resolution

Android: Showing wrong screen resolution

Finally after 2 days of searching and testing, i finally managed to find out what's the real issue here..
All those above coding will give correct resolution. But the most important this is that we need to mention the target SDK in the manifest file.. Otherwise it will give wrong result if the density is more than 1.

<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />

need to set the targetSDK version from anything between 4-9..
Hope this will help some one in future facing the same issue.

Android browser reporting the wrong screen size?

Many OEMs have chosen to set their default browser viewport dimensions based on those of the iPhone (or similar resolutions) despite having an altogether different resolution. Apple had a similar problem with the release of the 'retina display' on the iPhone 4 where the spec sheet states 640px across, but screen.width will return 320px when is set.

As @omermuhammed mentioned base your logic on screen.width, CSS @media queries AND/OR device detection using WURFL or DeviceAtlas.

The following article may also be of interest in helping to clarify the issue:

A pixel is not a pixel is not a pixel by @ppk
http://www.quirksmode.org/blog/archives/2010/04/a_pixel_is_not.html

Android DisplayMetrics returns incorrect screen size in pixels

Recent Galaxy S devices have a setting to change the screen resolution.
On the S20+ there are 3 options:

  • WQHD+: 3200x1440
  • FHD+: 2400x1080
  • HD+: 1600x720

You're likely in the FHD+ mode. Even though the device has a real pixel dimension of 3200x1440, your usable software window is downscaled and this is what the DisplayMetrics object is giving you.

Emulator wrong screen resolution in Android Studio 1.3

In the Android Virtual Device Manager click "Create virtual device", select needed one and click the "Clone device" button. Then change "Default skin" to "No skin". Now save the profile and launch it.

Android DisplayMetrics returns incorrect screen size in pixels on ICS

I found this hidden treasure for ICS ONLY......if you're targeting API's higher than ICS see the comment by Chris Schmich below.

How to hide and display the navigation bar on Android ICS build

In case the link dies....here's how to get the actual device screen size.....

Display display = getWindowManager().getDefaultDisplay();     
Method mGetRawH = Display.class.getMethod("getRawHeight");
Method mGetRawW = Display.class.getMethod("getRawWidth");
int rawWidth = (Integer) mGetRawW.invoke(display);
int rawHeight = (Integer) mGetRawH.invoke(display);

EDIT (11/19/12)

The above code WILL NOT WORK on Android 4.2 and an exception will be thrown....I have yet to find a way to get the full screen size of a device in Android 4.2. When I do, I will edit this answer, but for now, you should code with contingency for android 4.2!!

Wrong display width and height in Android

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.

How to detect the screen resolution with JavaScript?

original answer

Yes.

window.screen.availHeight
window.screen.availWidth

update 2017-11-10

From Tsunamis in the comments:

To get the native resolution of i.e. a mobile device you have to multiply with the device pixel ratio: window.screen.width * window.devicePixelRatio and window.screen.height * window.devicePixelRatio. This will also work on desktops, which will have a ratio of 1.

And from Ben in another answer:

In vanilla JavaScript, this will give you the AVAILABLE width/height:

window.screen.availHeight
window.screen.availWidth

For the absolute width/height, use:

window.screen.height
window.screen.width

Handle runtime screen resolution changes

Per the Screen Zoom documentation:

  • If an app targets API level 23 or lower, the system automatically kills all its background processes. This means that if a user switches away from such an app to open the Settings screen and changes the Display size setting, the system kills the app in the same manner that it would in a low-memory situation. If the app has any foreground processes, the system notifies those processes of the configuration change as described in Handling Runtime Changes, just as if the device's orientation had changed.

  • If an app targets Android 7.0, all of its processes (foreground and background) are notified of the configuration change as described in Handling Runtime Changes.

Similarly to handling configuration changes for multi-window, you should make sure you handle the following configChanges:

android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"


Related Topics



Leave a reply



Submit