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.
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
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.
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.
I've only placed mdpi and hdpi images inside each folder, but when I run my app on an ldpi device, it won't pick the images from the mdpi folder, but from the hdpi folder. Why is that? Any ideas on what i'm doing wrong?
P.S. If I place images specifically for the ldpi device inside the ldpi folder, it uses those, but if i delete them, it uses the hdpi ones.
Help.
That is the default behavior of Android. LDPI is basically half of HDPI and ANdroid basically picks images from HDPI, scales it to half the size and uses it.
Read the last note on this link
http://developer.android.com/training/basics/supporting-devices/screens.html
Android checks for the particular density.If the reference of the image is given than it checks for the availability of it and sets the image according to the density but if its not available than it will take the default image which resides in Hdpi folder which is the default behavior of Android.
Take an example of the default image of Android that is ic_launcher,if its present in a single folder,android is going to set it because it first find its availability than density.
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.