How to specify icons for different densities screen? - android

While working on an app, I noticed that there are different files such as drawable and drawable-hdpi. So I found out that these folders are for different icons and images size. My problem is that these two are two separate folders and I was wondering if I have to direct the android to the icons in the separate folder?
By that I mean do I have to first figure out what kind of dpi the device is and set the appropriate icons or can I just declare one drawable resource and android will figure it out for me?
If I can just set an icon name and the drawable file and the android system will know what to use for the different screen, then can you explain to me how this is done? I mean it is two separate files drawable and drawable-hdpi. So how can just set an icon image in the drawable folder and android knows what to access for the drawable-hdpi for the different device?

There are several questions bundled in one, but I will explain all of them in detail.
As you know, there are a variety of Android devices with different screen sizes. That is why, they are separated into a few categories based on their density. Must read this, if you haven't already.
This is the full classification,
So the idea is to have all these directories separately and not just a single "drawable" folder. Whenever you want to put any image or icon in your app, you need to put different sizes of them in their respective directories correctly.
two separate folders and I was wondering if I have to direct the
android to the icons in the separate folder?
You do not have to direct Android to do anything. Android will automatically pick the right image size from the "drawable-**dpi" intelligently.
By that I mean do I have to first figure out what kind of dpi the
device is and set the appropriate icons or can I just declare one
drawable resource and android will figure it out for me?
You can't figure out dpi of all devices, as your app will run on several devices having different densities. You just need to put different sizes of your icons in their respective drawable directory.
You should never put a single image or icon in the "drawable" directory as it can drastically slow down your app as Android need to dynamically resize your images at runtime.
If I can just set an icon name and the drawable file and the android
system will know what to use for the different screen, then can you
explain to me how this is done?
Just refer to the previous question, its already answered.
So how can just set an icon image in the drawable folder and android
knows what to access for the drawable-hdpi for the different device?
As explained earlier, Android obviously knows which device the app is currently running and thereby knows its screen density. Now it is very easy for Android just pick the right image based on the current screen density if you have put them properly in their respective drawable folders.
UPDATE
Just to answer your new question.
You start out with one "drawable" folder. But you need to create the other directories yourself and they need to have the proper name like drawable-mdpi, drawable-hdpi, drawable-xhdpi, etc.
You cannot have any other name like drawable-K or drawable-l, or not even a spelling mistake in the names. Android just scans to see if the folders with these names exists in your app, if so, then it will pick right one.
Suppose you are using a Nexus 5 which is an xxhdpi device,
i) Now Android will look for the drawable-xhdpi directory in your app for a particular image.
ii) If it exists, then it will just pick it up. If it doesn't then it will look for the nearest ones like drawable-xxhdpi or drawable-hdpi and resize the image slightly to fit.
iii) If nothing exists, then it will pick the image from the raw "drawable" folder.
Hope it clears all your doubts.

Related

why there are 2 drawable folder in res folder in android?

So I was pasting some image in the drawable folder and I found that there are 2 folders as you can see in below picture
one is drawable and another is drawable-v24
So my questions are
why there are 2 drawable folders ?
whats the diff. ?
Do we really need them ?
any additional information is deeply appreciated . :)
The drawable folder is the default folder. The drawable-v24 applies to only those devices with Android v24 or higher- any file in that directory will be used instead of the file with the same name in drawable on the applicable versions. Do you need them both? Only if you want different images on newer devices, or if there's some feature of v24 and higher that you want to take advantage of where available. If not you can safely delete it.
There are many drawable folder types based on many factors,
Android OS version, ex: drawable-V21, drawable-V22, drawable-v23...etc
Density pixels - Refer https://developer.android.com/training/multiscreen/screendensities
This different folder types are used to access the images for particular needs, say if you have to load a small size icon in Lollipop OS and a different size icon in all other OS, you have to use drawable-V21 for lollipop and normal drawable folder for all others.
Another case based on the resolution of mobile phones, quality will vary on images, example: drawable-hdpi images supports Full HD mobiles, whereas drawable-xxhdpi support QuadHD mobiles and so on..

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

Res folder and dpi?

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

Multiple MDPI/HDPI drawables with different for same

I am new to android and this concept of multiple resources is killing me and its Halloween =)
Ok so for Normal screen we have following HDPI per http://developer.android.com/guide/practices/screens_support.html#range
WVGA800 (480x800)
WVGA854 (480x854)
600x1024
so my image will go in drawable-hdpi. should image be 480x800, 480x854, 600x1024, or all 3 in the drawable-hdpi?
If all 3 are doing to be in drawable-hdpi, how I will name them? They can't have same names.
Thank you in advance
The common drawable folders are drawable-hdpi,drawable-mdpi,drawable-ldpi.
These folders are used to tell android which set of image to use on different situations.
The different names that can be used are given here.
When a small ldpi phone is being used and it tries to refer an image named icon.png. It first refers the drawable-ldpi folder. If it doesn't find it there, it moves to the other folders till it finds the image. But if the phone was an hdpi device, android would first look into the drawable-hdpi folder. So if icon should be of different sizes, you put the different sized image in each folder with the same name. And android will decide which folder to access the image in run time.
This might be a bit confusing in the beginning but you will get used to it after a while.

Why are my project's drawables blurry?

For some reason, the drawables in my app are blurry. This is especially apparent in menus when I put built-in system icons side-by-side with icons from my project's res folder.
Here's an example. The left envelope icon is in my app's drawable-hdpi folder. It is scaled down, for some reason. The right one is using the built-in Android resources. As far as I can tell, they are the same 72x72px file--I copied the png straight from the drawable-hdpi folder in SDK to my project's drawable-hdpi folder.
Is there some special setting for drawables in my app that I'm missing?
Try adding the appropriate supports-screens element to your Android manifest, and make sure you have a min-sdk of at least 4. Also make sure your uses-sdk element is nested under the manifest and not under the application (if it's in the wrong place or missing, your app will still work but can cause issues with compatibility-related code like resource scaling, and will prevent uploading to the market with an obscure error... it can be a tough one to track down).
Are you using an mdpi screen? If so then your device is having to take the drawables from drawables-hdpi and scale them down, which would likely cause them to blur.
You might try also copying the -mdpi resources to your drawable-mdpi folder, which would allow the framework use your drawables without adjusting for screen density.
Edit - now that I think about it, I'm not sure why your image would appear smaller unless your screen had a greater dpi than hdpi.

Categories

Resources