I am new to the concepts of android layouts associated with different screen sizes. I wish to make a different layout for a tab which is of 8" and place it in a "layout-swxxxx" folder. And have another generic layout for tabs bigger than than 8" in "layout" folder.
Here is the configuration of my 8" tablet.
Screen size 8.0 inches
Resolution 1200 x 1920 pixels (~283 ppi pixel density)
Can anyone tell me how to calculate the "sw", so that i can name my layout folder accordingly.
Also I don't have the physical device with me so its not possible for me to find this value with any code. So request an alternative method.
Thank you in advance.
The formula linking dp, pixels and density is as follow :
px = dp * (dpi / 160)
Therefore, in your case,
widthInDp = widthInPx / (dpi / 160)
widthInDp = 1200 / (283 / 160)
widthInDp = ~678dp
So you're gonna want to use a bucket such as sw600dp
Reference : http://developer.android.com/guide/practices/screens_support.html
Related
I am writing an Android game and I am testing it in two devices at the same time:
Huawei P10 Lite: 5.2", 1920x1080, 424dpi, xhdpi.
Huawei P40 Pro:
6.58", 2640x1200, 441dpi, xhdpi.
These two devices are utterly different, as they differ in screen size and resolution, but NOT in dpi and dpi bucket; both are xhdpi.
That means regardless the dimen files I create (dimen-mdpi, dimen-hdpi...), any graphic element looks the same in both phones, which completely breaks the layout and proportions in the small phone.
I'm taking this as a reference for bucket classification:
Well dpi is all about resolution, and not screen size. If you want to create resources for screen sizes, and not screen resolutions, you should use smalest width qualifiers.
According to the docs:
One DIP is one pixel on an
approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
providing the baseline of the system's display. Thus on a 160dpi screen
this density value will be 1; on a 120 dpi screen it would be .75; etc.
So if I understand correctly, you would convert from pixel to dp by: pixelSize / dip / 160. In this case the formula for your device width in dp is as follows: deviceWidthInPixels / (deviceDip / 160).
P10 width in dp: 1080 / (424 / 160) = 407.54
P40 width in dp: 1200 / (441 / 160) = 435.37
Looks like other people also came to this conclusion
I would like to know how can I add alternative resource for android devices with 5.5'' and 6' within a project structure.
I want to add a custom dimens.xml file with different text sizes for devices between 5.5 and 6 inches.
I was thinking to use the small width configuration qualifiers but there is so many screen configuration that I do not know which screen size would be the smallest width for 5.5'' and 6'' phones. The other alternative that I can use is the screen size buckets (small, normal, large, xlager) but those are deprecative from android 3.2 and above.
Can someone advice me about this?.
I appreciate your advice, but look this example.
I have this phone:
- Nexus 5x with 5.2’’ resolution 1080x1920 px, 420dpi
dp = px / (dpi / 160)
dp = 1080 px / (420 dpi / 160)
dp = 411 dp
Nexus 6p with 5.7 resolution 1440x2560 px, 560dpi
dp = px / (dpi / 160)
dp = 1440 px / (560 dpi / 160)
dp = 411 dp
if I create a specific set of resources just for the Nexus 6p with 5.7’’, or for devices with just 5.7’’ I could crate this folder
values-sw411dp
But all the resources within that folder will be used when the app is used on both devices 5.2’’ and 5.7’’.
I have other problem, it may be that smallest phones like, 5.0’’ could have biggest dp than the 5.7’’, what its going to happen is that the resources for the 5.7’’ device it will be used for the 5.0’’ devices too.
How can I specify resource just for 5.7’’ phones.
Well, you are right I'd also go with the smallest-width buckets.
You can calculate the smallest width, by checking the phones dimensions in dp, with the formula given in the official documentation:
px = dp * (dpi / 160)
So to get the smallest width dp:
dp = px / (dpi / 160)
Where px is the pixels on the smaller dimension, dpi is the dot per inch ratio of the device.
So first check the devices' pixel size and dpi value, then calculate the dp value with the formula above. Then you can create the folder with the resources like sw400dp where you can change to the calculated dp.
Also I'd suggest to check for other device dimensions to not have weird layouts on smaller / larger screens.
Abbath, thank you very mucho keep answering my questions. I am not working with images, I am working with the text size of a TextView. I need to change the textView text size, only if the devices screen size is bigger than 5.5''. I created a /value-sw411dp/dimens.xml which contain the corresponding textview text size. I found that sw411dp is the smallest dp width of the 5.5'' devices, but there is an other phone that has 5.2'' that has the same screen spaces. So, the system will used the resources within the /values-sw411dp/ for devices with 5.5''and 5.2''.
What you where telling me is that if a 5.2'' has the same screen dp as a 5.5'', I shouldn't worried about that because at the end the UI layout will look the same without problems.
I know this might be a silly question but I have really gone through so many materiel and links but still not quite understand it. In the "Supporting Multiple Screens" section of Android Develop Doc, it introduces dp like this:
Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way. The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
Basically I understand the fact that screen with higher dpi would have more pixels in a single physical inch, which means a dp in such a screen would equal more physical pixels(px).
But according to the above conversion equation (in bold font), in a screen with higher dpi (e.g. 240 dpi screen) , a px = (240 / 160) * dp = 1.5dp. This seems to mean that in higher dpi screen a px would equal more dp. This looks in conflict with my previous understanding.
So, please, could anyone help me to figure this tricky issue out. Thank you a lot, really.
You are looking at the wrong place in the formula. To see how many dp equals one px in different density, lets rearrange the formula:
px = dp * (dpi/160)
dp = px / (dpi/160)
Now for 1px, in mdpi devices:
dp = 1 / (160/160) = 1dp
In hdpi devices:
dp = 1 / (240/160) = 0.666666667dp
You can see that 1px equals less dp in higher density devices
I'm testing on a Samsung Galaxy Note 3 which according to the docs is 1080 x 1920 pixels (~386 ppi pixel density). I want to figure out what the screen width in dp is so I can properly apply and check the new size qualifiers.
I've measured the screen width (in portrait) with a good 'ole ruler = 71mm = 2.8 inches
1080 / 2.8 = 386 ppi. Great, this matches the stated density.
I'm trying to figure out how many dp units the screen is wide.
dp = px / (dpi / 160) from here
= 1080 / (386 / 160) = 1080 /
2.4125 = 447.7 dp
So if I do this:
<TextView
android:id="#+id/density_test"
android:layout_width="447dp"
android:layout_height="wrap_content"
android:background="#ffff0000"
android:text="DENSITY TEST 447dp" />
then on my Galaxy Note 3 this view should be almost exactly the width of the screen. But it isn't, it's way wider.
So I set the above view to layout_width="300dp", measured how wide it was with my ruler and extrapolated the actual screen width in dp, based on this test:
300dp = 59mm
Total screen width is 71mm
so extrapolating screen width in dp = (71 / 59) * 300 = 361dp
361dp != 447.7dp
What is going on here? I feel like I must be doing something dumb.
Edit: I tried putting the layout file inside /res/layout-xhdpi but it had no effect. I thought maybe it was scaling it by xhdpi / mdpi since I had the xml in the default /res/layout dir.
The actual pixel size is calculated based on dp, but not as accurate as the formula dp = px / (dpi / 160) does. Indeed, it first determines the screen density (ldpp, mdph, hdpi, xhdpi, xxhdpi, xxxhdpi) and using that density to get one ratio from {0.75, 1.0, 1.5, 2.0, 3.0, (4.0)}.
In you case, the system deems 386dpi as xxhdpi, so it calculates the value by multiplying 3.0, and 360dp would exactly fill the width of your screen (1080p)
This is probably an easy question, but i am just overlooking something.
I have an Android emulator with the following specs:
In my app I provide different layouts:
res\layout
res\layout-sw600dp
res\layout-sw720dp
But this emulator loads the regular layout.
I have another emulator with 160dpi and the rest of the specs are the same.
This one does load the sw720dp layouts.
Why doesn't the 10 inch 320dpi emulator loads the sw720dp layout?
From http://developer.android.com/guide/practices/screens_support.html
px = dp * (dpi / 160)
dp = px / (dpi /160)
With 1280x800 320dpi, sw = 800, than you get:
dp = 800 / (320 / 160) = 400dp
That is why it does not load res/layout-sw720dp or 600dp.
Solution would be to make sw400dp resources.