Res folder and dpi? - android

I just want to clarify, with the res folders in android, with lower resolution devices, they automatically get the same named asset from the ldpi folder? This is so I can make different assets for smaller screens?
Do I need to have assets in all 4 folders (ldpi - xhdpi) for it to work on all sized devices? Or can I just have assets in one folder if the assets are all the same for all devices?
And what if my assets are only different for low res devices, do I need to mirror the contents of each of the other folders so they all have the same assets in?

I would suggest reading this article thoroughly to get an understanding of exactly how it works, but basically, it will find the closest matching size resource that is available, so no, you don't need to mirror across all folders necessarily.
From the qualifiers section particularly, this quote explains it well.
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.

Lets break your question into three seperate questions.
I just want to clarify, with the res folders in android, with lower
resolution devices, they automatically get the same named asset from
the ldpi folder? This is so I can make different assets for smaller
screens?
Quite simply, yes. That's what the system is for. As long as you use assets with the same name, Android will automatically pick the one that works best for your device, depending on the dpi of the screen.
Do I need to have assets in all 4 folders (ldpi - xhdpi) for it to
work on all sized devices? Or can I just have assets in one folder if
the assets are all the same for all devices?
You do not need the assets in all folders to have it work on all devices. However, if you want it work well, you will need assets in all folders, all with their own dimensions depending on the folder. If you only use one folder with one size of assets, it will still work. Your assets will just get scaled by Android itself which can cause some assets to look really ugly on some higher/lower dpi devices. Let alone the fact that the scaling can cause your entire layout to mess up.
And what if my assets are only different for low res devices, do I
need to mirror the contents of each of the other folders so they all
have the same assets in?
By "only different for low res devices", do you mean that the entire asset is different? For instance a blue image instead of a red one? If so you would still need to use the same name as the assets in the other folders. The actual content can be different, the OS only checks the names when grabbing the assets.
For more info about how to support multiple screen sizes, give this guide a try:
http://developer.android.com/guide/practices/screens_support.html

Related

App Crashes. Error Inflating XML. Cant find image because xxxhdpi folder is not there

Switched my eclipse project to Android studio. I was maintaining resources under drawable-mdpi folder only. Now in studio the preview of XML loads images correctly. However when I run the app in a device with resolution higher than mdpi the app crashes, shows error inflating binary XML.
After a long analysis I found the issue that the device was trying to load images from its corresponding density folder which is not available. So I created the folder drawable-xhdpi and put images in that folder. Now the app works fine.
Why android studio can't pick image from other density drawable folder and resize which is possible by eclipse. I can't maintain 5 different drawable folders because there are lots of images.
you have to add "drawable-hdpi" resource directory and paste all the hdpi resources there because currently 70% android devices supports hdpi resolution images.
if you only maintain the hdpi, then it is also ok.
android manages all remaining resouces from hdpi resouce directory.
Android application resource directories provide different layout designs for different screen sizes and different drawables. These different drawables are used by android to support a major range of all the android devices present out there. It's a standard practice to put your resources considering these densities. Coming back to your query:
Why android studio can't pick image from other density drawable folder and resize which is possible by eclipse. I can't maintain 5 different drawable folders because there are lots of images.
For your case,In order to maintain this you could create a drawable with nodpi and put your all resources there. nodpi focus resources for all densities.Your resources should be density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen's density.
Hope this will clear your doubts, for more insight you can also look this.
you don't need to add all images to each difference size folder but depending the size of the image you might need to add images to different folders.
simple example is this can occour once you add high res/size images in normal drawble folder
Skipped 100 frames! The application may be doing too much work on its main thread.
This might not crash your app but will make it's performance down.
and
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 : https://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
Do we need to add all images with different dpi to Android Apps

Aliasing an entire resource folder with qualifiers

My app includes drawable resources for mdpi through xxhdpi. It's mainly intended for tablets, but it may occasionally be used on phones. The problem is that my drawables take up too much screen real estate on xhdpi phones.
I'd like to set up aliases so that xhdpi-small devices use the hdpi or mdpi drawables. I read the docs, but the examples there are pretty limited. Is there a way to alias specific resources with qualifiers, as opposed to resources in the unqualified drawable folder? And is there a way to alias the contents of an entire drawable folder, or do I have to alias each drawable individually?
Edit: I understand that I need to create a qualified resource folder. The question is about how to alias the resources in that folder to refer to resources in another qualified folder.
Make sure that you have put drawables in each folder with proper scaling ratio(3:4:6:8:12:16).If you still have problem on small screen size then provide separate layout for small and large screen sizes.

Avoid duplicate files in different android drawable folders

I'm developing an android game and will try cover as many different phone and tabled screen sizes and resolutions as possible. To make it easier to handle graphics I've designed some of my icons so that for example a 7" mdpi tablet uses same icons as an xhdpi phone.
That means I have put exactly the same icon file in drawable-xhdpi folder as in drawable-sw600dp-mdpi. I also have same files in drawable-land-xhdpi as in drawable-sw600dp-land-mdpi. It doesn't feel good to have duplicate files, can it be avoided somehow? Or how smart is eclipse compiler, can it detect duplicate files and compress the apk file accordingly?
You can avoid duplicating image files by using aliases. An image alias is an XML resource file containing a <bitmap /> tag. Look at the documentation for the syntax.
In your case I think you should name your resources differently for phone and tablets if they don't end up having the same density-independent size. From what I understand, your tablet images are bigger than your phone images, which is generally not a good idea.

App crash on devices with ldpi screens

I created an app with images, and I added them via Android Icon Set wizard. The wizard created 3 versions for the images - mdpi, hdpi, xhdpi.
I published my app on google play, and I got a crash reports from users that have ldpi screen. The exception was Caused by: android.content.res.Resources$NotFoundException: File from drawable resource ID #0x7f02007f.
The drawable was exists in mdpi, hdpi and xhdpi and the app worked to the rest of the users.
So I guess that the problem was that the drawable dose not exists in its ldpi version.
So my questions are:
1) Is there any way to say to the app to auto scale the mdpi drawables instead of crashing?
2) For support ldpi screens, must I edit the images to ldpi size?
Thanks.
This is a common error of different densities management, so these are the answers to your questions.
1.- No, in the way your assets are stored but Yes, "ONLY if your asset is in a lower hierarchy of your current density" for example: If you have assets in drawable(default non specific density) and drawable-ldpi AND you run the app in a Medium Density Device, the OS will try to resize your Images from -ldpi to your density (if using dps in the Image but will cost memory). The way the OS handles the Assets is the following:
Lets say you have:
res-
-drawable
-icon.png
-drawable-ldpi
-icon.png
-drawable-mdpi
-icon.png
If you are running an app in a HIGH Density device the OS first will try to find your asset in drawable-hdpi, but since it doesn't exist it will start going down the hierarchy until it finds it, so in this case it will not find it in drawable-hdpi but will find it in drawable-mdpi and will use that one to populate, everything will be fine but will cost in memory the difference of densities in the assets.
Now the exact error you have comes to play when the OS goes down the assets folder hierarchy and goes until the end and do not find anything for example:
Lets say you have:
res-
-drawable-hdpi
-icon.png
If you run this app, in HIGH Desnity Device it will run perfect, because will find the asset in the first try and will never go down the hierarchy, same case for Extra High Density Device because if it doesnt find it in drawable-xhdpi it will find it in the next step when going down the hierarchy to drawable-hdpi and will work just fine, but for MEDIUM Density, however, first it will try to find it in drawable-mdpi since its not there, will go down and try to find it in drawable-ldpi no luck either so it will go to the "default (drawable)" which is a good practice to contain all the assets in an average size to at least make the app look blurry than crashing, since the OS will not find the Asset either BOOOM no resource found, there you have your exception, this mechanism applies for pretty much any resource in Android
2.- YES, you must create your assets in ldpi and store them either in drawable-ldpi or drawable(default - no density) in order to make it look good.
All this information has been taken out of a book and if you still have doubts, create an empty Android Project in Eclipse and notice how the SDK creates one icon_launcher.png img in each density with a specific size to handle exactly this issue. As a recommendation in my experience I've found useful to always take care of all the densities, but more important having all my assets with an average density/quality in the default folder to avoid this exact issue in case you could forget one density during development, is better a blurry asset than a crash.
Hope this Helps.
Regards!
Is there any way to say to the app to auto scale the mdpi drawables instead of crashing?
This happens automatically, unless the maker of the firmware (device manufacturer or ROM modder) screwed it up.
For support ldpi screens, must I edit the images to ldpi size?
No.
I would look up 7f02007f in the R.java associated with your production code base and make sure that it is what you think it is. Bear in mind that these numbers get regenerated on every compile. Perhaps this is a case where there simply is no drawable resource for this number, in any density, because R.java was out of sync with the actual resource packaging. To avoid this problem, do a clean build (e.g., Project > Clean inside of Eclipse) as part of making the production APK.

Is there any possibility that android device screen size does not match with any of generalized size

Is there any 0.00001% chance that android device screen size does not match with any of four generalized screen sizes?
If yes, from where will it pick the layout resources, if I don't have defined them in default layout folder?
Note I have default layout folder res/layout/ but don't have all the screens defined in this folder rather defined them in corresponding layout-'screen size' folder.
Is there any 0.00001% chance that android device screen size does not match with any of four generalized screen sizes?
All devices that legitimately have the Android Market on them will be in one of the four size buckets, or possibly in new buckets defined as part of future versions of Android.
However, there are devices (e.g., WIMM One wearable) that run Android and do not have the Android Market on them. Such devices could, in theory, do something unusual.
If yes, from where will it pick the layout resources, if I don't have defined them in default layout folder?
You would have to ask the device manufacturer.
I have default layout folder res/layout/ but don't have all the screens defined in this folder rather defined them in corresponding layout-'screen size' folder.
Generally speaking, it is best to have one of everything in res/layout, then override them with tailored layouts only where needed (e.g., in res/layout-large-land/). If nothing else, this minimizes code duplication.

Categories

Resources