Samsung Galaxy S8+ : Device Metrics Gives wrong device screen pixel density - java

This is how I am getting the screen density using Android Studios
float density = getResources().getDisplayMetrics().density;
According to device specification, S8+ has 4.0 Density value and falls under xxxhdpi category
Refer this site https://material.io/devices/,
Have Attached a screenshot for reference
here
But the value returned by the above code is 2.9, which seems very wrong
check here
Tried using densityDpi also, but it also returns xxhdpi as opposed to xxxhdpi
int densityDpi = context.getResources().getDisplayMetrics().densityDpi;
The above code works well on other devices, tested on OnePlus 5T, Nexus 6P, Redmi Note 4, Moto g4+ and others...facing issues with the Galaxy s8+, haven't tested for Galaxy s8 but i guess the result will be same.
Is this a know Bug, or am I doing something wrong?
Its getting difficult to manage layout for S8+ devices without the correct pixel density info for s8+ devices.

Just like Surfman said. Your phone isn't at the maximum possible resolution. If you take the resolution reported within your screenshot and divide it by the reported 2.9 and multiply it by 4 you end up with roughly the maximum resolution of 2960x1440

You need to initialize display metrics object
You can try this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
flat density=metrics.density;
flat densityDpi=metrics.densityDpi;

Related

Android different layouts for 4.0, 4.5 and 5.0 devices

I need different layout for device 4.0 , 4.5 and 5.0.
But I checked screen size width using:
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpWidth = outMetrics.widthPixels / density;
And on
Sony Xperia Sp I have W: 360.0 H: 592.0
Sony Xperia Z I have the same result W: 360.0 H: 592.0
but screens have differents sizes and layout looks really diffrent.
How to create different layouts for this and other devices sizes correctly?
EDIT:
In this case layouts look really different because one of phones had changed FONT SIZE in Device Settings.. GG Well played testers : )
Thx all. Up vote.
Android uses density independent pixels (dp), so it should be quite easy to adapt to different sizes. In your res folder create subfolders like layout-w320p and customise your layout inside that folder to adapt to different screen sizes.
Always try using RelativeLayout instead of FrameLayout and position items relative to the device. Item sizes should be made wrap_content so that it will automatically be shrinked to the device.
You can provide different layout based on exact width in pixels since you already know them:
Examples
res/layout/main_activity.xml # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)
res/layout/main_activity.xml # For handsets (smaller than 600dp available width)
res/layout-w600dp/main_activity.xml # Multi-pane (any screen with 600dp available width or more)
Read the official android documentation about Supporting Multiple Screens
Sony Xperia Sp I have W: 360.0 H: 592.0Sony Xperia Sp I have W: 360.0 H: 592.0
Sony Xperia Z I have the same result W: 360.0 H: 592.0.
This is not screen size.. It totally depends on your device screen resolution.
If maintain this screen resolution you should create different layout or value file as
value-sw320dp-hdpi
value-sw320dp-xhdpi
value-sw320dp-xxhdpi
OR
drawable-sw320dp-hdpi
drawable-sw320dp-xhdpi
drawable-sw320dp-xxhdpi

Dimension values taken from wrong folder

I have made several values folder and dimen.xml files for configuring my app for different screen sizes. The preview in Android Studio shows them perfectly however, when I run the app on the phone, those dimensions don't take effect. And the layout shown in the Android Studio layout and the layout shown on the phone are different.
So for example on my nexus 5 the app should read dimensions from values-w640dp/dimen.xml however, it reads them from values/dimen.xml for some odd reason.
What can I do to resolve it?
Here are the screenshots.
Android Studio Preview Snapshot (Nexus 5)
Nexus 5 Phone Screenshot:
So dp are calculated using the following formula:
pixels / dp = dpi / 160dpi or pixels / dp = density
Looking at device metrics, the Nexus 5 has a density of 3 so:
dp = pixels / density
dp = 1920px / 3 = 640dp
dp = 1080px / 3 = 360dp
Using values-sw600dp would not fix it because that is not the actual smallest width of the screen since it can also be 360. So you should use values-sw300dp. Be warned though that this covers a very wide variety of devices. values-sw600dp usually targets tablets.
I have also run into issues where the true resolution of the device does not match what the emulator created, with the Nexus 5 too, so make sure you check the AVD screen and ensure that the resolution matches the real resolution of the device. Example, the Nexus 5 should be 1920x1080 and it is. If you ever encounter this issue, recreate the emulator or try a different one entirely.

Folder name for 7" hdpi tablet Android

I have Tablet with 7" screen (600×1024) with hdpi (240 dpi classification).
I have created folder layout-sw600dp. But it's not working in this resolution tablet.
Its working fine with 7" screen (600×1024) with mdpi (160 dpi classification).
Which folder should I create for 7" (600×1024) tablet which has hdpi (240 dpi classification)?
It depends from the Android API version you're building against, like mentioned here:
... However, this won't work well on pre-3.2 devices, because they
don't recognize sw600dp as a size qualifier, so you still have to use
the large qualifier as well. So, you should have a file named
res/layout-large/main.xml which is identical to
res/layout-sw600dp/main.xml. In the next section you'll see a
technique that allows you to avoid duplicating the layout files this
way.
You should also take a look here:
Preparing for Handsets
and
New Tools For Managing Screen Sizes
Make Your Layout like this:
res/layout/main_activity.xml # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)
For TAB:
For example, if your application is only for tablet-style devices with a 600dp smallest available width:
<supports-screens
android:requiresSmallestWidthDp="600" />
I have Tablet with 7" screen (600×1024) with hdpi (240 dpi classification) which is comes under the Normal Screen see my screen shot.Its working fine with 7" screen (600×1024) with mdpi (160 dpi classification) which is comes under the large screen.
For Tablet .
MULTIPLE SCREENS:
For example, the following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for medium, high, and extra high density screens.
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
Hope this will help you.
In android, we use resolution in dp to measure the screen size, not resolution in px.
Both of your two tablets have the same resolution in px, but their resolution in dp are quite different.
600 X 1024px with mdpi = 600 * 1024 dp
600 X 1024px with hdpi = 400 * 682 dp
You use sw600dp as the qualifier for tablet, which will effect the first device but not the second one.
In fact, the second device(400 * 682dp), is much more like a handset rather than a tablet, it should not use the layout for tablet.

drawable sizes for different screen sizes in android

i have a problem in android development that bored me. my problem is screen size and dealing with that. specially i have some problems with images. for example i want to create a background image for my activity that i created in photoshop and my background image contains a "HELLO" word on it. but when i put it on drawable-xhdpi folder, it seems blurry and its not sharp!! my phone is a nexus 4 and according to Google documentation i create background image in 640 x 480 size.
when i create background image in 960 x 720 size it seems better but not perfect. in this case my image file size is very high!
but what is the standard way for this? please help me to solve this problem for ever. i read google documentation but its not solve my problem!
http://developer.android.com/guide/practices/screens_support.html
You should usually avoid creating images for certain screen sizes to make them background, because there are thousands of different devices and you would have to create dozens of such images.
The first thing you need to be aware of is screen density.
Generally you create 3 to 5 images when not even looking at screen size: low (120 dpi), medium (160 dpi), high (240 dpi), extra high (320 dpi) and 2*extra high (480 dpi). These go into drawable-Xdpi folders, where X is one of l, m, h, xh, xxh.
Next thing when you want to have bigger images on bigger screens (bigger phones, small and big tablets), you may want to put images to folders like drawable-sw600dp-Xdpi. This is not a case for your phone.
Nexus 4 is a xhdpi 640x384 dp device, but you should not treat it differently than Samsung Galaxy S2 (hdpi 533x320 dp).
Create an image of smaller size for both phones and center it horizontally. E.g. 320x100 px for mdpi, 480x150 px for hdpi and 640x200 px for xhdpi (your phone).
the screen resolution for Nexus is 1280x768 (http://www.google.com/nexus/4/specs/), resize the image to this resolution. In especial consideration some images can't handle the resolution and the image became disproportionately.
for interesting
resolution calculator:
http://members.ping.de/~sven/dpi.html
This is problem of Android Fragmentation and you just cannot deal with it perfectly as there is a several hundreds different devices. As colleague above wrote Nexus 4 has resolution -1280 x 768 so for sure res of image as equal as 960 x 720 is good choice. I'm even surprised that google suggest 640 x 480 for xhdpi, it's definitely too less.
So as I said you are not able to make perfect looking graphics for all existing devices. You should choose the most popular devices from every screen category(xhdpi,mdpi,ldpi ... etc) to cover the most important market share.
With 1600+ android models even after they are categorized in few Screen size and a few DPI's its very difficult to manage layouts.. i suggest that you just concentrate on designing layouts w.r.t to screen size and then create views as Resizeable Views to neglect density effects.
Once you have created your layouts Resize the Views .. You can create a Custom View or resize on its onMeasure();

Is current device tablet?

Is there a way to determnine if current device tablet in Android 2.2 and higher?
Currently I'm using minimal screen dimension, but there is some tablets (Viewsonic ViewPad7 480x800) with resolution like phones or even smaller (e. g. HTC Evo 3D has 960x540).
Is there any hardware property to read?
There is no reliable difference between phones and tablets, it is only possible to point it using specific features, the below can be used in conjunction with each other
The String Build.VERSION.RELEASE will give you the user-visible version string (i.e 1.5, 1.6, 2.0), while Build.VERSION.SDK_INT will give you a value from Build.VERSION_CODES. Remember Build.VERSION.SDK_INT works on Android 1.6 higher but not for 1.5. Build.VERSION.SDK will work on 1.5 or higher.
http://developer.android.com/reference/android/os/Build.html
http://developer.android.com/reference/android/os/Build.VERSION.html
And DisplayMetrics describes general information about a display
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
You can use getResources().getConfiguration()
For example:
getResources().getConfiguration().navigation
The following code will return the actual resolution of the physical device.
Display d = getWindowManager().getDefaultDisplay();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
int h = d.getWidth();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
int w = d.getWidth();

Categories