1

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?

Wowzer
  • 1,103
  • 2
  • 12
  • 26

1 Answers1

5

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,

enter image description here

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.

Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
  • Thanks for the thorough answer but I still have something that need to be clarify. If android knows how to pick the right one does that just mean I have to just direct the image source to the drawable folder and android will pick it? Also, what is there to prevent me from naming something like drawable-randomdpi and specify a qualifier. Will android know what to pick then? – Wowzer Jun 02 '16 at 05:39
  • Btw the article you send me, I am in the process of reading that but I still didn't quite understand so I asked the question here. – Wowzer Jun 02 '16 at 05:40
  • I have explained this earlier as well. just go through my answer. You just need to create different sizes of your image or icon an put them in the drawable folders - drawable-hdpi, drawable-mdpi,, drawable-xxhdpi, etc. Android will pick the right one at the right time. – Aritra Roy Jun 02 '16 at 05:41
  • Now I understand. However, I feel like my second question is not answered very well because what I don't know is how can android knows what to pick in a different folder. In android studio, I originally start out with just a drawable folder with no drawable-hdpi or anything like that. So let me clarify my second question. If I create another folder lets said drawable-z or drawabl-K will android still knows what to pick? What is there to said that android should used these folder for different densities? – Wowzer Jun 02 '16 at 05:45