I unable to get the values from "values-sw480dp" nor on my sony Xperia device which is 480 x 854 neither on Samsung galaxy tab which is 600 x 1024. But able to get on Motorola Xoom which is 1280 x 752.
e.g res/values-sw480dp/strings.xml/
<string name="hello_world">hello world</string>
The values on respective devices
Sony Xperia ----> #2131034113
Samsung ----> #2131034113
on xoom ---> hello world
Can any one explain me this behavior, why Sony Xperia and Samsung has "#2131034113" value.
Thank you in advance
According to http://developer.android.com/guide/topics/resources/providing-resources.html, sw480dp means «smallest side of screen must be at least 480 dp». Now dp is a «pixel on a 160 dpi screen», so actual size of e.g. Xperia in dp would be I guess 320 x 569 (provided it's a hdpi device).
To get device screen size in dp you need to know abstracted density class (ldpi=120, mdpi=160, hdpi=240, xhdpi=320). For the above Xperia screen the calculation is: (480*160/240) x (854*160/240), where 160 is the base density (it's where 1 dp actually equal to one pixel) and 240 is device abstracted density.
Related
I have 2 questions about the "DPI" term.
1st, i googled this term, someone said that it means dots per inch. and we can use follow formula to canculate the screen dpi:
dpi = radical(screen_width_pixel^2+screen_height_pixel^2)/screen_inchs
Now, i have an android tv, i found that my tv resolution is 1920*1080, and the dpi is 240, the tv size is 50 inch. But if i use above formula to calculate the dpi, i will get the dpi value is 40, i don't know why? is above formula ppi formula?
2nd, i found that the dpi value is in the build.prop->ro.sf.lcd_density=240, i don't know why the manfacturer set this value, in other words, what is this value based on, why not setting 320 or other values? They user above farmula to get the value and set it?
Thanks a lot!
I have been researching answer to your question for last one day, and reached some conclusions -
1.
PPI is pixels per inch, purely mathematical. i.e. - it calculate number of pixels per inch.
DP - Device Independent Pixels AKA dots -> i.e. android system defines a virtual pixel so that it could compare devices with different pixel densities. You know baseline is 160 DPi (160 dots per inch), so any MDPI device has a virtual grid which contains 160 DP per inch, while a device which is HDPI has same grid but it will contain 240 DP per inch. (Note that number of pixels one dot/Device independent Pixel may contain varies, depending on the size of device.)
here comes your answers-
a) DPi is system defined, all you can calculate is number of DP in one inch, which in your case sums up to - 1920*160/240 =1280 DPs in height, 1080*160/240 = 720 DPs in width), what you calculated is PPi.
b) manufacturer set it so that you could know how much you need to resize your image. Suppose an image which is of certain size in 160DPi, then to make this image look same size, you need to double the size of the image on 320DPi screen.
Note - I tried on my part to make you understand, if you find any problem understanding my answer, Please comment, would be happy to assist.
Reference # 1
Try this information Iuser lastly to develop and android studio 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
-
res/values/dimens.xml(default)
res/values-ldpi/dimens.xml (240x320 and nearer resolution)
res/values-mdpi/dimens.xml (320x480 and nearer resolution)
res/values-hdpi/dimens.xml (480x800, 540x960 and nearer resolution)
res/values-xhdpi/dimens.xml (720x1280 - Samsung S3, Micromax Canvas HD, etc)
res/values-xxhdpi/dimens.xml (1080x1920 - Samsung S4, HTC one, etc)
res/values-large/dimens.xml (480x800)
-
res/values-large-mdpi/dimens.xml (600x1024)
res/values-sw600dp/dimens.xml (600x1024)
res/values-sw720dp/dimens.xml (800x1280)
res/values-xlarge-xhdpi/dimens.xml (2560x1600 - Nexus 10")
res/values-large-xhdpi/dimens.xml (1200x1920 - Nexus 7"(latest))
w600 says if width of current orientation is nearby 600
sq600 says if smallest with of device is nearby 600
I want to distinguish phones with ~5 inch and higher displays from 4 inch 480x800 in my res/values folder
How can I differentiate ~5 inch displays?
I should probably use this trick, but what should got instead of XXX?
values-swXXXdp?
Especially I am trying to differentiate Galaxy S 3 and Galaxy S 4 from others. What should I consider?
S3:
DPI: 306
RES: 1280x720
Screen: 4.8"
720 / (306/160) = 720 / 1.9125 = 376.47 dp
S4:
DPI: 441
RES: 1920x1080
Screen: 5.0"
1080 / (441/160) = 1080 / 2.75625 = 391.8367 dp
So basically there is a very small difference between S3 and S4. But if you want to distinguish layouts for these devices, use sw376dp for S3 and sw390dp for S4. But make a note that other 5" phones could be different.
I am using "layout-sw360dp" for both Galaxy S4 and Nexus 4 devices but in my screen displaying some space on Galaxy S4. so can i make different layout for both Galaxy S4 and Nexus 4. if it is possible then please give me the solution.
For Galaxy s4 use layout-xxhdpi
For Nexus 4 use layout-xhdpi
Galaxy S4 -- 1080 x 1920 pixels -- 5.0" -- 441 ppi -- xxhdpi
Nexus 4 -- 1280 x 768 pixels -- 4.7" -- 320 ppi -- xhdpi
You have to make different images to fit to the resolution of these devices
As per other answers one device is 640 x 360 dp and the other 640 x 384 dp.
You don't want to make a different layout for them, because you will sooner or later see a device 640 x 372 dp or 640 x 388 dp.
Instead make your layout elastic and don't hardcode pixels (density independent pixels can also be badly hardcoded).
ok here goes...
for reference
this is older layout folder naming conventions but you get the point.
You must convert everything to mdpi to go into the following buckets.
buckets for layouts are :
xlarge 960 x720 min
large 640 x480 min
normal 470 x 320 min
small 426x 320 min
scaling ratio
3:4:6:8:12
this means that you must multiple ldpi * 3/4 to go from ldpi to mdpi
and you must multiple mdpi * 4/6 to go from mdpi to hdpi
and you must multiple hdpi * 6/8 to go from hdpi to xhdpi
and you must multiple xhdpi * 8/12 to go from xhdpi to xxhdpi
so to answer the question....
s4 is xxhdpi which means the scaling ratio is 4/12 or 1/3. This means that you have to convert from xxhdpi to mdpi to place in a layout folder properly.
so 1080 * 1/3 = 360
and 1920 * 1/3 = 640
so this would place it in a normal layout folder
nexus 4 is xhdpi which means the scaling ratio is 4/8 or 1/2. 4 This means that you have to convert from xhdpi to mdpi to place in a layout folder properly.
so 1280 * 1/2 = 640
and 768 * 1/2 = 384
so this would place it in a normal layout folder as well.
ok, so this doesn't quite cut it because they are different resolutions. we can make up folders with the new naming conventions. for example the smallest width for the s4 is 360 while the smallest width for the nexus 4 is 384. so folders could be sw360dp and sw384dp.
you could also name the folder layout-xxhdpi which would catch the s4 but not the nexus 4
be aware: Just to throw another one out there what if you had an s3 as well. that would go in the sw360dp folder as well. so I guess you would have to differ between them as well. if you used layout-xxhdpi above then you could use sw360dp here and sw384dp above as well. cover them all.
thanks
Both these phones have different screen size and resolution , that leads to different density as well .
As per my knowledge -
Galaxy s4 has 1080 x 1920 pixels, 5.0 inches (~441 ppi pixel density).
And
Nexus 4 has 1280 x 768 pixels, 4.7 inches (320 ppi ).
You will have to design the layouts (and images as well ) according to the phones specifications.
for nexus 4 use below layout.
layout-sw360dp
both galaxy s3 and nexus 4 have same layouts.
I have 2 different android phones. The details are
nexus s: 480 x 800 pixels, 4.0 inches (~233 ppi pixel density)
desire s: 480 x 800 pixels, 3.70 inches (~252 ppi pixel density)
Now I have my layout folders as layout_hdpi and layout_xhdpi. Now both these phones are classes as hdpi phones. I have a controls with a padding of 5dip to the left and the right. But on these phones the 5dip translates to different widths hence the border on one phone is fatter then on the second phone. Increasing one also increases the other. Can someone help me please?
for different screen sizes use relative-layouts.. it will not cause the problem of different width on different phones
First of all layout_hdpi and layout_xhdpi should be declared as layout-hdpi and layout-xhdpi.
Secondly, HDPI refers to the range 161–240(ppi). So your Nexus phone is eligible for the HDPI qualifier, but your Desire S is eligible for XHDPI qualifier, because the range of xhdpi is 241–319.
I am trying query xdpi and ydpi value of Motorola Droid2 global and Motorola Atrix to calculate screen width, These are the values i am getting
Atrix:
Resolution: 540*960
xdpi: 160
ydpi: 160
calculated screen width is coming to 6.1, whereas originial value is 4
Droid 2:
Resolution: 480*854
xdpi: 144
ydpi: 144
calculated screen widht is coming to 6.9, whereas original value is 4.
Same logic of width calculation works fine with Xoom, HTC, samusng phones and some other android devices. In those cases xdpi and ydpi reported are around 240
Are values 144 and 160 correct values?
If this is not the right approach any idea, how can we calculate screen width programatically?