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.
Related
I've been developing for about 5 years and have always used 'dpi' folders in my res directory (mdpi, hdpi, xhdpi, etc) with 'sw' folders for tablets (sw600dp, sw720dp, etc).
I've noticed in Android Studio that, starting with the Nexus 6, google lists its newer devices in sw buckets rather than in dpi buckets.
I know we can still use dpi folders but SHOULD we? Or should we be transitioning to sw folders for all device sizes?
Or does it not matter?
google lists its newer devices in sw buckets rather than in dpi buckets.
First, those are independent concepts:
sw is "smallest width" and refers to size
dpi is "dots per inch" and refers to screen density
Also, there is no occurrence of "sw" in that screenshot that I can see. So, I do not know what is worrying you.
One guess is that you are looking at values like 420dpi and xhdpi and thinking that they are different concepts. They are both densities. The Android Studio UI is listing finer-grained density buckets than we use for our resources. xhdpi is 320dpi, for example.
Another guess is that you are looking at values like 420dpi and thinking that means -sw420dp, which is not the case.
I know we can still use dpi folders but SHOULD we?
For bitmaps, yes. For anything else, no.
Or should we be transitioning to sw folders for all device sizes?
Using size-based folders — -wNNNdp, -hNNNdp, or -swNNNdp for varying values of NNN — may be necessary depending on your UI. If you need different layouts or margins or padding or something based on screen size, those folders are useful.
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.
I am researching best practices for working with layouts in Android projects.
In the Android documentation says that from Android 3.2 to create qualifiers type -mdpi -hdpi etc is obsolete and that the best way is to create specific folders for each screen as sw600dp and etc. Also talks to create different layouts for each case, but how are the dimensions and images in this approach?
I create separate layouts for each case with fixed dimensions in the layout itself? How is that?
Images continue in folders like "dawable-mdpi"?
I spent some time studying the documentation but doubt continues ..
https://developer.android.com/guide/practices/screens_support.html
Correct me if I am wrong.
Before Android 3.0 drawable were placed in folders depending on the screen size. For supporting larger devices, putting drawable according to screen density was introduced. With this drawable were chosen depending on the pixel density of the screen and not just the size of the screen.
You can read about it in "How to Support Multiple Screens" section of this page https://developer.android.com/guide/practices/screens_support.html#support
So I think using small, normal, large, and xlarge is depreciated and we now use ldpi (low), mdpi (medium), hdpi (high), xhdpi extra-high), xxhdpi (extra-extra-high), and xxxhdpi (extra-extra-extra-high)
And sw600dp, sw720dp etc are for devices with screen dimension at least 600dp, 720dp etc. You can read more about it here: https://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts
You'll find answers to all your question in the documentation you spent some time studying. Go through it again, and I am sure things will be a lot more clear to you.
I'm very confused about various icon sizes required in android. Do I really need to have smaller icons for xxhdpi,xhdpi,hdpi,mdi if I've provided an icon for xxxhdpi and I want to show same icon for every resolution?
Since we are playing in dp why can't android just downsize them?
PS: I tried having only xxxhdpi icons and they looked perfectly fine on smaller screens for my use case.
You should read this link.
Just to summarize:
Using a size qualifier does not imply that the resources are only for screens of that size. If you do not provide alternative resources with qualifiers that better match the current device configuration, the system may use whichever resources are the best match.
Caution: If all your resources use a size qualifier that is larger than the current screen, the system will not use them and your application will crash at runtime (for example, if all layout resources are tagged with the xlarge qualifier, but the device is a normal-size screen).
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.