What order does Android read values folders? - android

In my Android project I have multiple values folders with extensions such as -hdpi, -xhdpi, and -sw720dp. In all of these folders I have dimens.xml and the same names for values in that xml file. If I have a device that has a width of 720 dp and is counted as an xhdpi device (as an example), then from what folder does Android read the values from? Also, in general, how does Android prioritize the different values folders?

Also, in general, how does Android prioritize the different values folders?
Generally speaking, Android takes the resources from the resource set that has the resource in question and has the most important distinct qualifier. Here, "most important" means "appears highest in Table 2 on this Web page", and "distinct" means "nobody else has that particular qualifier". Though things get a bit weird with screen sizes and densities.
I have multiple values folders with extensions such as -hdpi, -xhdpi, and -sw720dp. In all of these folders I have dimens.xml and the same names for values in that xml file.
IMHO that is a serious code smell. Use density-independent units of measure (e.g., dp), in which case you should not need different dimension resources based upon screen density. Having different ones based upon screen size is reasonable (e.g., bigger margins on tablets), though the mix then would be something like res/values/dimens.xml and res/values-sw720dp/values.xml.
If I have a device that has a width of 720 dp and is counted as an xhdpi device (as an example), then from what folder does Android read the values from?
-swNNNdp is higher in Table 2 than is -hdpi or -xhdpi, and so it should be considered first. Note that the size resource set qualifiers (e.g., -large, -swNNNdp) are for the specified size or larger. So, if your device has 720dp or more in the smallest width, your -sw720dp resource should be used.

Related

1080x1920 420dpi screen not accessing from sw420dp

I am creating different values folder for supporting different screen sizes.The following are the folders:
values-sw320dp-hdpi
values-sw320dp-xhdpi
values-sw320dp-xxhdpi
values-sw420dpi
values-sw560dpi
values-sw480dp-hdpi
values-sw480dp-xhdpi
values-sw480dp-xxhdpi
values-sw600dp
values-sw720dp
In the list of devices provided by Android Studio there is 1080x1920 420dpi.It should access dimens values from values-sw420dp.But instead it is accessing from sw320dp-xxhdpi.The layout in 1080x1920 420dpi **looks slightly different from **1080x1920 xxhdpi. Can anyone explain to me why it is not accessing from its own folder?Or could you explain to me the correct way to create a layout so that it support different screen sizes with different densities?I have referred different sites.They are confusing..Please help me!!
I am creating different values folder for supporting different screen sizes.
First, using density qualifiers (e.g., -xhdpi) on resource types other than drawable and mipmap is a serious code smell. Almost assuredly, you are not going to get the results that you expect.
Second, there is no -sw420dpi or -sw560dpi qualifier. You could have -sw420dp or -sw560dp, to say that you want to use these resources for those screen size thresholds. However, such directories would never be used, because of your density qualifiers on directories like values-sw320dp-hdpi.
In the list of devices provided by Android Studio there is 1080x1920 420dpi.It should access dimens values from values-sw420dp
No, it should not.
The smallest width of that screen is 1080px. 1080px, at 420dpi, is 411dp (1080 * 160 / 420). 411 < 420. Hence, anything that is -sw420dp will not qualify.
But instead it is accessing from sw320dp-xxhdpi.
Partially, that is because 411 is lower than 420.
Partially, that is because you are using density qualifiers here, which short-circuit a lot of the "normal" rules for resource selection.
The layout in 1080x1920 420dpi **looks slightly different from **1080x1920 xxhdpi.
Your question has no layouts, so nobody can comment on that.
Can anyone explain to me why it is not accessing from its own folder?
There is no "its own folder".
Or could you explain to me the correct way to create a layout so that it support different screen sizes with different densities?
Use layout resources based on screen size and (perhaps) orientation (e.g., res/layout-sw420dp/)
Use dimen resources measured in sp (for sizing text or things that are dominated by text) or dp (for everything else) in those layout resources (e.g., <dimen name="margin">16dp</dimen>)
Use density qualifiers, and maybe size qualifiers, for drawable and mipmap resources (e.g., res/drawable-hdpi/)
Frequently, that is sufficient.
I have referred different sites.They are confusing
Perhaps consider reading a book.

Different Text Sizes for different Screen Sizes, using what ratio factor?

Hello I am using different textsizes depending on different screen sizes.
Like:
values-small: dimens.xml -> ?
values-normal: dimens.xml -> 10sp
values-large: dimens.xml -> ?
values-xlarge: dimens.xml -> ?
I want to know if there is a formula to set the right sizes depending on small, large, xlarge instead of setting sp size by testing it on different screens^^?
second question: is values-normal neccessary, isnt "values" folder == values-normal?
third question: what happens when I declare dimens.xml for small, normal, and large and the device is a XLARGE device will it use dimens.xml from values? or valuse-normal? or the next smaller one values-large?
thank you
I want to know if there is a formula to set the right sizes depending on small, large, xlarge instead of setting sp size by testing it on different screens
Not really. Many apps do not change their font size at all based on screen size, just as most Web apps do not change their font size based on browser window size. Of course, you are certainly welcome to specify different <dimen> resources for different screen sizes, including dimensions for use with text. And there's nothing stopping you from using an algorithm like the others suggest; just understand that there's no real reason to use that algorithm. To put it another way, a graphic designer should be telling you how big to make the text, not a calculator.
Moreover, the -small, -normal, etc. buckets are not used as much anymore, in favor of the more flexible -wNNNdp, -hNNNdp, and -swNNNdp buckets.
isnt "values" folder == values-normal?
No.
Suppose you have res/values-small/, res/values/, res/values-large/, and res/values-xlarge/. Further suppose that each resource set defines a text_size dimension resource, and you use that in in layouts (e.g., #dimen/text_size).
A -normal device will then pull from res/values-small/, as -small is for small screens or larger. Your res/values/ version of the resource will never be used. Hence, the typical pattern would be not have res/values-small/, putting your -small resources in res/values/, and overriding that default value in res/values-normal/, res/values-large/, and res/values-xlarge/.
what happens when I declare dimens.xml for small, normal, and large and the device is a XLARGE device will it use dimens.xml from values? or valuse-normal? or the next smaller one values-large?
It should pull from res/values-large/, as that is the closest match among the qualifying resource sets.
This is the common problem in majority of Android projects to support varied screen sizes.
In short, you can make use of scalable DP and scalable Font approach.
Android project structure inherently allows specifying different font size (SP) and margin values (DP) by defining them in dimens.xml files for supported screen sizes. But the problem that still remain is - what values to be used ? UX designers normally provide style guides for only one set (e.g. 360x640 DP screen size) and don't provide for all other supported screen sizes. So the problem to use right vlaues for other supported screen sizes still remain unanswered.
One good solution (per my understading) is to use readymade scalable dictionary of dimens.xml files, something similar to the dictionary of files provided by this library. After adding these xml files to project, you just need to use the keys in your layouts, then Android automatically uses font/margin value from respective directory. Please refer here for visual difference - with and without this approach.
I use the ratio 0.75 : 1 : 1.5 : 2 same as for DPIs. It works fine.

In Android, why do you need the resource qualifier -mdpi if it is already the default?

Say you have a dimens.xml file that goes in the res/values folder of your project. Then, you want to have some different dimensions based on screen density. So, for example, you can create res/values-hdpi/dimens.xml and res/values-xxhdpi/dimens.xml and have specific data for those configurations.
My question is, is there ever a reason to use res/values-mdpi? As I understand it, mdpi is the default density. Any values in the default values folder is considered mdpi so why is there ever a need for having values-mdpi?
Although I can think of a reason why you might have extra qualifiers such as values-en-mdpi, I don't see how values-mdpi is ever necessary when you can just put them in values.

How to determine which resource file is being used and why?

I'm testing one of my layouts using the different devices defined in the graphical layout editor in Eclipse. Due to some of the devices' varying resolutions I had to define different dimens.xml files to define specific font sizes so the design would look correct.
I have a dimens.xml file in each of the following resource folders:
values
values-sw1280dp
values-sw640dp
values-sw800dp
values-sw800dp-large
For whatever reason the device 7in WSVGA (Tablet) uses the dimens.xml in the values folder. But according to the device configuration the resolution for this device is 1024 x 600, so shouldn't it be using the dimens.xml defined in values-sw800dp-large?
Anyone have any idea?
It won't use values-sw800dp-large because that qualifier means that the smallest dimension must be at least 800dp. Also note that these are dp values, not pixels. So if the device is hdpi, 800dp is 1200 pixels.
If you're doing this in an emulator, there is a weird interaction with device display density and scaling of the emulator display. So, for instance, if your device is supposed to be hdpi, it may actually be treated as an mdpi display unless you specify a scaling.

Delveloping for Android high res screens, but supporting low res screens

I've been developing Android games for the lower res screens (320x480) and then letting Android handle the density adjustments so the games work on high res screens. It allows me to keep the size of my APK files small and I don't have to create multiple versions of every image in my games. It actually works fine, the games still look nice on higher res screens.
However, my question is, is it possible to do the same thing, only backwards? Can I develop for high res screens (800x480) and then somehow let android handle the density reductions automatically?
Yes.
In your xml layouts, use dip values for sizes.
Example:
100dip =
75 # 120dpi
100 # 160dpi
150 # 240dpi
if you need a higher res resource for certain drawables, use the...
drawable-hdpi
drawable-mdpi
drawable-ldpi
The app will use the drawable from the correct resource folder depending on the screen's density.
Try not to use absolute widths and heights on certain elements if you can use FILL_PARENT or WRAP_CONTENT.
Here's some good tips:
http://developer.android.com/guide/practices/screens_support.html
Yes, and it works the same. You put your resources in the appropriate directory and the system will downscale them accordingly.

Categories

Resources