Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the Android drawable directory contain subdirectories?

In the Android SDK documentation, all of the examples used with the @drawable/my_image xml syntax directly address images that are stored in the res/drawable directory in my project.

I am wondering if it is explicitly not okay to create a sub directory within the drawable directory.

For example, if I had the following directory layout:

res/drawable -- sandwiches   -- tunaOnRye.png   -- hamAndSwiss.png -- drinks   -- coldOne.png   -- hotTea.png 

Could I reference the image of a tuna salad sandwich as @drawable/sandwiches/tunaOnRye

Or do I have to keep the hierarchy flat in the drawable directory.

like image 200
Pepper Lebeck-Jobe Avatar asked Jul 03 '09 00:07

Pepper Lebeck-Jobe


People also ask

Can the Android layout folder contain subfolders?

First, Android supports only a linear list of files within the predefined folders under res. For example, it does not support nested folders under the layout folder (or the other folders under res).

What will be in the drawable folder of Android?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.

What types of files can be stored in the RES drawable directory?

Supported file types are PNG (preferred), JPG (acceptable), and GIF (discouraged). App icons, logos, and other graphics, such as those used in games, are well suited for this technique. To use an image resource, add your file to the res/drawable/ directory of your project.

What folder is Drawables directory in?

In the Android SDK documentation, all of the examples used with the @drawable/my_image xml syntax directly address images that are stored in the res/drawable directory in my project.


2 Answers

No, the resources mechanism doesn't support subfolders in the drawable directory, so yes - you need to keep that hierarchy flat.

The directory layout you showed would result in none of the images being available.

From my own experiments it seems that having a subfolder with any items in it, within the res/drawable folder, will cause the resource compiler to fail -- preventing the R.java file from being generated correctly.

like image 197
Reto Meier Avatar answered Sep 19 '22 23:09

Reto Meier


The workaround I'm using (and the one Android itself seems to favor) is to essentially substitute an underscore for a forward slash, so your structure would look something like this:

sandwich_tunaOnRye.png sandwich_hamAndSwiss.png drink_coldOne.png drink_hotTea.png 

The approach requires you to be meticulous in your naming and doesn't make it much easier to wrangle the files themselves (if you decided that drinks and sandwiches should really all be "food", you'd have to do a mass rename rather than simply moving them to the directory); but your programming logic's complexity doesn't suffer too badly compared to the folder structure equivalent.

This situation sucks indeed. Android is a mixed bag of wonderful and terrible design decisions. We can only hope for the latter portion to get weeded out with all due haste :)

like image 20
Cheezmeister Avatar answered Sep 19 '22 23:09

Cheezmeister