Android: Values priority - android

In my app in the res folder, there are values folders for different devices. For example:
values-mdpi
Values-hdpi
values-v14
values-v17
values-w480
values-w720
etc.
I wonder what is the priority of these folders for Android?
Let's imagine couple devices:
first : 480*800 hdpi v14
second: 320*480 mdpi v17
third : 720*1280 hdpi v17
What folder would Android use for each of them?

You can find the priority of resource folders here in this page. Basically it is used to support different devices and configurations.
Android uses a certain logic to determine the best possible matching resource folder for a device. This is explained in this documentation page.
Regarding your devices query:
First: 480 * 800 hdpi v14 -- values-hdpi
Second: 320 * 480 mdpi v17 -- values-mdpi
Third: 720 * 1280 hdpi v17 -- values-w480
In the list provided by you, values-w480 (devices with lowest width of 480dp, only from API 13) has got the highest merit. So whichever device meets that criteria, it'll take resources from that folder.
The values-vXX (devices with API >= XX) has got the lowest merit. So if the other folders are not taken, then only Android takes resources from this folder. Check the table listing the resources qualifiers for more info on that topic. The resource qualifiers are listed in the table in the order of precedence of resource qualifiers.
First case: Normal hdpi - less than 480dp width -- So values-hdpi
Second case: Normal mdpi - less than 480dp width -- values-mdpi
(values-mdpi has got more weightage than values-v17)
Third case: hdpi device with width of 480dp (720/1.5 = 480) API 17 --
values-w480

This is what the Android documentation states:
Be aware that, when the Android system picks which resources to use at
runtime, it uses certain logic to determing the "best matching"
resources. That is, the qualifiers you use don't have to exactly match
the current screen configuration in all cases in order for the system
to use them. Specifically, when selecting resources based on the size
qualifiers, the system will use resources designed for a screen
smaller than the current screen if there are no resources that better
match (for example, a large-size screen will use normal-size screen
resources if necessary). However, if the only available resources are
larger than the current screen, the system will not use them and your
application will crash if no other resources match the device
configuration (for example, if all layout resources are tagged with
the xlarge qualifier, but the device is a normal-size screen). For
more information about how the system selects resources, read How
Android Finds the Best-matching Resource.
Source
How Android Finds the Best-Matching Resource

Related

App loading wrong layout from folder

I have a default layout\ folder for all my layouts and layout-hdpi\ for just one that is not displaying properly on lower resolutions. Problem is that my emulator with xxxhdpi and xxhdpi (haven't checked xhdpi) is accesing layout.xml from wrong folder layout-hdpi\. Why and how to fix it (besides creating layout-xxxhdpi\ and layout-xxhdpi\ for the same file)!?
That's a very interesting behaviour, but as the documentation describes:
Note: Using a density qualifier doesn't imply that the resources are only for screens of that density. If you don't provide alternative resources with qualifiers that better match the current device configuration, the system may use whichever resources are the best match.
And if you look a little bit further onto the chapter How Android finds the best-matching resource you will find the following:
Exception: Screen pixel density is the one qualifier that is not eliminated due to a contradiction. Even though the screen density of the device is hdpi, drawable-port-ldpi/ isn't eliminated because every screen density is considered to be a match at this point. More information is available in the Supporting Multiple Screens document.

Which comes first, values-sw or values-dpi?

I want to know which has the highest priority, sw or dpi, as you all know I can provide some android resource folders according to both of them, for example there is the res/value folder which can be provided according to the device smallest-width or device density in the form of res/values-sw600dp and res/values-hdpi respectively, So, if I have a device which will match these two, which value would it take? the one in the hdpi or the one in the sw600dp.
This table lists all of the qualifiers in order of precedence.
The smallest-width qualifier has higher precedence than the screen pixel density qualifier.
You may also be interested in the How Android finds the Best-matching Resource documentation.

Is it possible to target Specific Android phones using the /res/values folder

I'm developing an app that targets only Android Phones, not tablets. The app should work in portrait mode only. I have all the dimensions (for views and fonts) in a dimens.xml file in the values folder.
I want to adjust the appearance for some commonly used phones, for example
Galaxy S2: 240dpi, 480x800
Nexus 4: 320 dpi, 768x1184
Nexus 5: 480 dpi, 1080x1800
I want to specify some font size and view dimensions specifically for these targets. I've tried with a lot of different values folder names, for example:
values-sw240dp
values-sw480dp
values-sw320dp, and so on,
but the app uses always the same folder for the three devices (normally the 480dp folder).
It is possible to specify dimensions in a resource file targeted for these devices?
Thank you in advance!
PS: excuse my bad English.
Yes it is possible. Every single folder inside the /res/ folder works exactly the same regarding those filters (xhdpi, sw600dp, en, land, etc).
The problem on your case is that you did not specify the correct folders. And the correct folders depends on what is the parameter you're using to specify the different dimensions.
sw___dp is for "smallest width", meaning, the smallest width of the screen (in a 800x480 phone is the 480 side) is that amount of DPs.
to specify DPIs you probably want to use hdpi, xhdpi, xxhdpi. Which will fit into those 3 devices you mention.
If you really want to use sw___dp, you probably want to install THIS APP on the devices and it shows you the sizing in DP for each screen (alternatively, if you don't have the devices, you'll have to do some math.
here is a comprehensive documentation of all the qualifiers you can use for resource folders.
It seems the best you can do is to use the dpi classes, and actually, I think that it's more correct cause you are optimizing you resources for every device in that class instead of only a specific few leaving the others to unknown results.
Not for specific devices, but for device sizes, it is possible using value folders named like these (width-height):
values-w480dp-h800dp

Do we need to add all images with different dpi to Android Apps

As you know Android applications have different DPIs and also in the Android applications we can add drawables with diffrent DPIs in diffrent ldpi, mdpi, hdpi and xhdpi folders.
The question is that when we want to support all DPIs we should add the icons with all different sizes or just with the biggest one?
I mean for example suppose that I have one action bar item with icon. Whether I should add the icon of this action bar item with 24x24(in drawable-mdpi folder),36x36 (in drawble-hdpi folder),48x48 (in drawable-xhdpi folder),.....
Or I just need to add one icon with size 96x96 in xxxhdpi folder and android will set the icon for other DPIs with good quality?
TLDR see the bold below
Different density folders were added later on for Android which means that...
If you wanted to be lazy and just add one asset the best choice would probably be the HDPI asset if your min app target < 8 and XHDPI if its >= 8. This is because the system will scale the resource up and down, but you would still want to start off with the highest resolution possible.
If you want to have complete control over how the assets are scaled then you can by all means provide your own for all / some of the densitys. In practise I generally provide HDPI / XHDPI as above and give all the resource buckets for things like logos / AB icons / App icons etc. I generally find the auto scaling to be pretty good and work for most situations, but will occasionally have to supply and extra LD/MD asset if its a small asset / contains small text etc. Plus if i duplicated all assets for things like XXXHDPI I would get pretty good apk bloat.
You can also use IDEs built in tools to add a single asset for many densitys at once. In Android Studio 0.6 this is File->New->Image Asset and a wizard will appear.
I have never noticed or heard of any perfomance impact of allowing Android to scale assets automatically - presumably this is done in hardware.
It may not look great when auto scaling down to LDPI say so you can optionally provide your own scaled assets for all other densities.
Taken from the link below
ldpi: Low-density screens; approximately 120dpi.
mdpi: Medium-density (on traditional HVGA) screens; approximately 160dpi.
hdpi: High-density screens; approximately 240dpi.
xhdpi: Extra high-density screens; approximately 320dpi. Added in API Level 8
nodpi: This can be used for bitmap resources that you do not want to be scaled to match the device density.
tvdpi: Screens somewhere between mdpi and hdpi; approximately 213dpi. This is not considered a "primary" density group. It is mostly intended for televisions and most apps shouldn't need it—providing mdpi and hdpi resources is sufficient for most apps and the system will scale them as appropriate. This qualifier was introduced with API level 13.
List taken from this dev link for more info.
This is the approach I have used on many apps in my professional career including ones for Google & the BBC and not had issues.

Android default density values when the current is missing

I had a question about densities and how Android chooses the right values folder for the current density. I think I've found the answer but I am not completely sure so I'll post what I've found. At least, this post may help someone.
For example, if I only include folders values, values-hdpi and values-xxhdpi, what does Android do when the current density is xhpdi?
It seems that Android looks for a higher density folder, so it will use values-xxhdpi.
And what happens when the current density is xxxhdpi?
It seems that, if Android can't find a higher density folder, it looks for a lower one, so it will use values-xxdpi too.
Android will never use the default values folder for a value that is defined in some specific values-*dpi folder.
To sum up, supposing I only have values, values-xxhdpi and values-hdpi:
Values in values-hdpi will be used for devices with densities ldpi, mdpi and hdpi.
Values in values-xxhdpi will be used for devices with densities xhdpi, xxhdpiand xxxhdpi.
Values in values won't be ever used if they are defined in any values-*dpi folder.
Let me know what you think about this.
Thanks
density qualifier is an exception of how resource matching works.
Screen pixel density is the one qualifier that is not eliminated due
to a contradiction. Even though the screen density of the device is
hdpi, drawable-port-ldpi/ is not eliminated because every screen
density is considered to be a match at this point.
Providing Resources
when the system is looking for a density-specific resource and does
not find it in the density-specific directory, it won't always use the
default resources. The system may instead use one of the other
density-specific resources in order to provide better results when
scaling. For example, when looking for a low-density resource and it
is not available, the system prefers to scale-down the high-density
version of the resource, because the system can easily scale a
high-density resource down to low-density by a factor of 0.5, with
fewer artifacts, compared to scaling a medium-density resource by a
factor of 0.75.
Supporting Multiple Screens
So in case of density Android may use the default folder, but may also decide to use one that is not an exact match. Of course the substantiation is about drawable resources, but the mechanism probably works the same also for all other resources. Otherwise there could be a problem with drawables not matching other resources because they were chosen using different rules.

Categories

Resources