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

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.

Related

Limit layout to only hdpi density

I have a layout that I want to show ONLY on hdpi screens. Using the density qualifier hdpi does nothing and I still see the layout on larger devices. Is the qualifier not working or am I misunderstanding something?
From the Android docs we see that:
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.
Basically, the system tries to find a better resource, but the one designed for hdpi is the best. If you want to use that only on hdpi, then you should probably create alternative resources for mdpi and xhdpi to be used for the rest of the screens.

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.

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

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.

Android: Values priority

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

Categories

Resources