App loading wrong layout from folder - android

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.

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.

About Android layout

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.

Do I really need small icons when I've provided an icon for xxxhdpi in android?

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 am confused with drawable-hdpi and drawable-mdpi

i am attempting to get my android app to work on both phone type devices and tablets. for the layouts i have layout-normal and layout-large and i place my xml files inside each and it seems to select the correct one for each device (layout-normal for phones and layout-large for tablets).
however the problem arises for the drawable folders. i have drawable-hdpi, drawable-mdpi and drawable-ldpi. the tablet is taking its images from the drawable-mdpi, and the phone is taking its images from the drawable-hdpi. from the reading i have been doing this seems to be backwards??? this seems to happen on both the emulator and real devices, could i be doing something wrong? am i mixing something up?
Clayton,
drawable-hdpi and drawable-mdpi is not related to the size of the screen itself, but to the resolution of the display.
It's probably easy to think in terms of the iPhone 3GS and 4 for this (bear with me please). The 4 has retina display, hence a much higher resolution. Using the same sprites you'd use with the 3GS would result in a blurry display.
Android phones are, if anything, much more diverse in terms of screen options.
If you want to be able to aim different layouts at different resolutions, please try putting the layouts into /layout-xlarge, /layout-large, etc...
You can find more here:
http://developer.android.com/guide/practices/screens_support.html
There are some changes that you can see for Android 3.2 and above:
Beginning with Android 3.2 (API level 13), the above size groups are
deprecated and you should instead use the swdp configuration
qualifier to define the smallest available width required by your
layout resources. For example, if your multi-pane tablet layout
requires at least 600dp of screen width, you should place it in
layout-sw600dp/. Using the new techniques for declaring layout
resources is discussed further in the section about Declaring Tablet
Layouts for Android 3.2.
ldpi, mdpi, and hdpi refer to pixel density, not screen size.
The accepted answer does not address a serious problem: if you have many drawables, you end up with an unnecessarily big apk file because of resource duplicating. The only solution for that is the resource alias option described here:
http://blog.evendanan.net/2011/03/Android-resources-and-device-fragmentation
Edit: I just beheld that OP didn't realize that most phones have hdpi density, while most tablets have mdpi density. At the same time the tablets mostly have higher resolution. Hence Android will use smaller pictures for higher resolution.
You can overcome this only by using vector images or using the methods described in the accepted answer and in my answer.
drawable-hdpi - application use this drawable images when the device has high pixel density
drawable-mdpi - application use this drawable images when the device has medium pixel density
drawable-ldpi.-application use this drawable images when the device has low pixel density

Android - Screen Resolution and Screen Density

As per managing resources(images) in > Android 1.6 version, we need to keep different-resolutions image in Drawable-Hdpi, Drawable-Mdpi, Drawable-Lpi folder particularly.
And as per this page: http://developer.android.com/guide/practices/screens_support.html ,
In Low density section - there are three resolutions used: 240*320 , 240x400 , 240x432 for the Small screen, Normal Screen, Large Screen particularly.
same way for Medium density section - there are three resolutions used: 320x480 , 480x800 , 480x854 for the Small screen, Normal Screen, Large Screen particularly.
and same way for High density ........
but i am confused here:
(1) How do i come to know that whether small, Normal or Large screen is used, i mean is there any way to know ?
(2) How do i come to know which type of density i am using ?
(3) And in Drawable-Hdpi, Drawable-Mdpi, Drawable-ldpi folder, which resolution's image we should keep particularly?
There are specific Android API calls that can, at runtime tell you what density and (small/large/normal) screen size a handset has. However, as a developer we should not need to worry about individual handsets at all. All we need to do is to have ldpi/mdpi/hdpi assets and small/normal/large layouts in the apk. Android internally handles everything.
Dont forget to get an indepth understanding of how Android determines which assets to use and aliasing here.
Why do you want to know the actual density? It's Android's business. But I'm sure there is a way to retrieve this information.
For development I put everything in the hdpi-folder. I also could put everything in a general Drawable Folder.
At the time u publish u can decide if u want to provide already downscaled resources for ldpi and mdpi. However, thats not necessary.
Update: Retrieve actual density with this class and best practices
Update 2: I found a 25 min video from Motorola discussing all those issues: Working with multiple screens
1) Change the content of the layout in different folders i.e layout-small, layout-large, etc Now test it in Different emulator with different screen resolution.
2) For Finding out density of the Device use
Log.d("Density", "" + (getResources().getDisplayMetrics().density));

Categories

Resources