I have the following drawable folders
/drawable/
/drawable-hdpi/
/drawable-ldpi/
/drawable-mdpi/
/drawable-xhdpi/
If I have an image resource, do I need to create 5 (or 4?) different resolution versions of the file? Let's say I make just two versions and put them in their respective folders:
/drawable/
/drawable-hdpi/image.png
/drawable-ldpi/
/drawable-mdpi/image.png
/drawable-xhdpi/
What happens when someone on a LDPI or XHDPI device visits an Activity that needs to display image.png? Does it just not show it? Does the app crash? Or does Android follow a resource cascading system, show the version that is nearest to the current screen density?
Also, in this case I don't have an image.png in the /drawable/ folder. Is this bad? Should every single resource have (at minimum) a version in the /drawable/ folder?
Also, if you have specified all 4 screen density drawable folders (xhdpi, hdpi, mdpi, ldpi), then what is the point of the normal /drawable/ folder? When will it ever get used?
If I have an image resource, do I need to create 5 (or 4?) different
resolution versions of the file?
No, you don't have to
What happens when someone on a LDPI or XHDPI device visits an Activity
that needs to display image.png? Does it just not show it? Does the
app crash? Or does Android follow a resource cascading system, show
the version that is nearest to the current screen density?
Android uses a nice cascading system for its resources. This page explains it a bit. Afaik LDPI devices use the MDPI resource if a LDPI resource isn't available, XHDPI uses HDPI resources. (I could be a bit off, I can't recall the exact rules) Your app doesn't crash.
Your app crashes if you only have higher resolution resources than the screen can take.
Also, in this case I don't have an image.png in the /drawable/ folder.
Is this bad? Should every single resource have (at minimum) a version
in the /drawable/ folder?
No, not necessary.
Also, if you have specified all 4 screen density drawable folders
(xhdpi, hdpi, mdpi, ldpi), then what is the point of the normal
/drawable/ folder? When will it ever get used?
The drawable folder is for images that don't need scaling, and things like shape drawable resource XML files etc.
Related
There are multiple groups of screen densities of Android phones (ldpi, mdpi, hdpi and so on).
I had a folder "drawable" with picture size 1920 x 930 pixels which is for xxxhdpi, I guess.
I want to arrange pictures for all screen densities from xxxhdpi to hdpi. So, I should rename my existing folder: "drawable" -> "drawable-xxxhdpi". Then, I should create 3 additional folders for others. Do I understand it right?
How would actually the program choose proper folder?
For example, I also have smaller pictures now. Should I also resize them? If I don't duplicate them in new folder, will the program crash?
Will the program crash on phones with, for example, hdpi if I don't create a folder for hdpi?
Couldn't get how it works, would be grateful for an explanation.
First of all be sure that you are in the "Project" view because you will not see all the folders if you are in "Android" view. Then instead of renaming drawable folder create the others and keep drawable for some files that doesn't need to resize (for example defining a background). Folders should be:
drawable-hdpi
drawable-mdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi
Selection between them will be done automatically according to the screen of the device you are installing the app. If screen is xxxhdpi and you don't have a file for that resolution the app will take the smaller and so on. Obviously this will not be a great UI performance but you should not have problems with crashes.
There are many online App icons generators in the web so you can take a big icon and put it there so you get the icons for all sizes
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
I want my app to be as lightweight as possible. So i put all my images inside drawable-xxhdpi and let the Android OS to resize them.
However for the 10 inch mdpi tablets i want to use hdpi images. I know, that it is possible to create drawable-sw720dp-mdpi and put there hdpi images. But this will increase apk size, which is not the option. So, is it possible to make such a change without making drawable copies?
create a drawable folder in res folder of project and paste all images there...it will resize all images according to device size
Let's say you have a set of resources called icon_text.png; and you place one in the drawable-xxhdpi folder and another one in drawable-hdpi folder. That is your scenario, right? Any device that is mdpi (it doesn't matter its size) will pull resources (if available) from the hdpi folder. The system will pick the closest match, and mdpi is closer to hdpi than to xxhdpi.
Simple question. How does android work with drawable folders if any folders are empty ? Lets say I have images only in folder "drawable-hdpi". When I start application on emulator with higher dpi "drawable-xhdpi" it load images from "drawable-hdpi" (of course in bad size). Also when I start application on device with lower dpi "drawable-mdpi" it load images from "drawable-hdpi".
My question is - does it mean if doesn't exist images for current device dpi, android system takes images from other folder it is not empty ? Till today I thought, when images in current dpi are not present, application doesnt work.
It takes the near dpi, and scales the image up or down by the dpi difference between the target dpi and the folder dpi. If in the "drawale" folder (no dpi), no scaling takes place.
http://developer.android.com/training/basics/supporting-devices/screens.html
Note: Low-density (ldpi) resources aren’t always necessary. When you provide hdpi assets, the system scales them down by one half to properly fit ldpi screens.
It works. It first check in the current dpi folder for the resources. If not found then it use other available resources
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.