Android : screen density confusion? - android

I've developed a test app to understand android screen density related things. I've values.xml and strings.xml having respective test data in following folder structure :
values\dimen.xml (100dp)
values-mdpi\dimen.xml (100dp)
values-hdpi\dimen.xml (150dp)
values-xhdpi\dimen.xml (200dp)
values-xxhdpi\dimen.xml (300dp)
I am showing these data on UI to know from which folder app is taking data for differetnt screen sizes and densities. So, i'm getting following result :
1st device
2nd device
Problem is 2nd device with xxhdpi is showing me 900dp value, but in values-xxhdpi\dimen.xml value is 300dp?

900 is a pixels value. Your One PLus 2 have density 3, it means 300dp == 900px

If It didn't pick up the correct layout use values folder like this
values
values-ldpi
values-mdpi
values-hdpi
values-xhdpi
//added folders
values-w300dp-mdpi
values-w600dp-mdpi
Add the folders as required

Sony Xperia J has 245 dpi screen and OnePlus 2 has 401 dpi screen, so there's something wrong with these values.
Densities are as follows:
ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi
So it's correct that your app takes images from hdpi for Xperia J and xxhdpi directory for OnePlus 2. Read more here: http://developer.android.com/guide/practices/screens_support.html

Related

How to make TextView's text size based on device screen size in android

i have one app which can be runs on different screen size from 3.7 Inches to 6.0 inches(no plan for tablet now).
now i have TextView which have 17dp for title and 14dp description. but when i run the app in small device (3.7-4.7 inches screen size device) the textview's text is big.
But the same textview in higher 5.5 or 5.7 this can be small .
so how can i make TextView looks same for all device.
I had the same issue and I used this library and it did the thing for me.
The thing is you just have to replace sp with ssp in your xml after adding this library in your gradle and you'll be good to go.
As screen size is between 3.7 and 6.0 you should only create 1 additional dimens files like sw480 and use dp-s for TextView dimensions and sp for text size. In default dimens file you should write sizes for devices with smallest screen sizes e.g. for 3.7‘, and comparably bigger dimens in sw480 file e.g for 5.5‘ screens. Based on particular cases additional dimens files can be created like sw360. If screen is less than 360 dp width,it will use the values from default dimens file, if 360dp <= screen size < 480dp then sw360 will be used. The same simple logic works for each created sw dimens file.
Create multiple dimens.xml for your project, it will give size according to the display size
You Have to create multiple values folder and have to create dimens.xml inside them.
A reference for those folders are:
values-sw720dp 10.1” tablet 1280x800 mdpi
values-sw600dp 7.0” tablet 1024x600 mdpi
values-sw480dp 5.4” 480x854 mdpi
values-sw480dp 5.1” 480x800 mdpi
values-xxhdpi 5.5" 1080x1920 xxhdpi
values-xxxhdpi 5.5" 1440x2560 xxxhdpi
values-xhdpi 4.7” 1280x720 xhdpi
values-xhdpi 4.65” 720x1280 xhdpi
values-hdpi 4.0” 480x800 hdpi
values-hdpi 3.7” 480x854 hdpi
values-mdpi 3.2” 320x480 mdpi
values-ldpi 3.4” 240x432 ldpi
values-ldpi 3.3” 240x400 ldpi
values-ldpi 2.7” 240x320 ldpi
In android you are dealing with dp/sp values. Don't think of physical size.
There's this nifty plugin that lets you convert your sp/dp values to different buckets. You can add
https://plugins.jetbrains.com/plugin/9349-dimenify

Should we use the exactly DPI number to implement for multiple screen size?

I know we have some standard of DPI that we can use it for support multiple screen size in an like :
ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi
But when I calculate for some real device, e.g. LG G3 - it has DPI is 538.
So my question is do we need to use that exactly DPI number for scale the image,... or just let it be one of xxhdpi (480dpi) or xxxhdpi (640dpi).
For my opinion, I think you shouldn't care about information dpi of devices. You should only focus to provide images, size (dp,sp) etc for some type of screens :
ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi
=========================================================================
Example:
As you maybe known that 1 px = dp (dpi/160)
So if you have an image with size is 32 x 32 px on screen 160 dpi. It's mean your image get size 32 x 32 dp.
Now you want to scale it on xxhdpi (extra-extra-high) ~480dpi.
On xxhdpi (extra-extra-high) ~480dpi : 1 px = dp(480/160) = 3dp
Then you only need to provide another image with size 32*3 x 32*3 px = 96 x 96 px
You just have to provide the right assets and android will take care of the rest. You can check the documentation for more details.
The Android system helps your application achieve density independence in two ways:
The system scales dp units as appropriate for the current screen density
The system scales drawable resources to the appropriate size, based on the current screen density, if necessary
So in your case its better to provide both the assets. At runtime, the system uses the appropriate resources for your application, based on the generalized size or density of the current device screen.

Android - how to adapt layouts for different screen sizes

I provided different xml layouts (with same name) for different screen sizes - like layout-sw320dp, layout-sw480dp etc.
But when I try to change some properties in the specific layout in order to adapt UI for different devices - for example layout_height, layout_width, paddingRight, topMargin and so on - it still doesn't look good.
I suppouse that's because there is a screen sizes range (4-5") and different screen sizes read from layout-sw320dp. I tried on Galaxy S4 - 5" screen and on Galaxy S3 Mini - 4" screen. So on S4 evrything looks good but on S3 mini - doesn't - buttons are moved down, image not in the center.
What am I doing wrong?
Thank you in advance!
Use dimens.xml to assign Widths,Margins,padding. This xml is available in res/values folder. for different devices u can create different dimens. It simalar to having differnt Images for differnt Devices.
Source
You have to create Different values folder for different screens . Like
values-sw720dp 10.1” tablet 1280x800 mdpi
values-sw600dp 7.0” tablet 1024x600 mdpi
values-sw480dp 5.4” 480x854 mdpi
values-sw480dp 5.1” 480x800 mdpi
values-xhdpi 4.7” 1280x720 xhdpi
values-xhdpi 4.65” 720x1280 xhdpi
values-hdpi 4.0” 480x800 hdpi
values-hdpi 3.7” 480x854 hdpi
values-mdpi 3.2” 320x480 mdpi
values-ldpi 3.4” 240x432 ldpi
values-ldpi 3.3” 240x400 ldpi
values-ldpi 2.7” 240x320 ldpi
Check this Image :-

Values for 240dpi screen

i want to create a folder values, only for 240dpi screens, and if it is bigger that that so use other folder like 480..
The "values-sw240dp" folder is not working to me.
My main goal is to set different dimens values for 240dpi screens.
Thanks ahead.
You are using wrong folder names.
Dpi densities qualifiers have following values
ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi
You should name your folder values-hdpi
Using configuration qualifiers
values-sw240dp means screen smallest width is 240dp

How to provide different resolutions for header image?

This is a follow up question to this post. I have a header image on one of my screens and want to scale it to the full width but keeping the aspect ratio. So it should do something like this:
I can do that, but now I have the problem that the image scales relatively bad. (Imagine some text on the picture above, this is not the best example). How can I provide several pictures in different resolutions to android so that android take the pictures which is best for the current screen size/density situation?
Android support different screen densities through the use of configuration qualifier as follow:
ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi
The best practice is to create a directory under the res/drawables/ directory with those qualifier. Eg.: /res/drawable/hpdi and store an image of the appropriate resolution in that folder. Do the same for the other qualifier; the proper image resolution will be chosen automatically based on the screen density of the device. Image filename must be the same in each of the configuration qualifier directory.
More info: Android Developers documentation.

Categories

Resources