views:

2785

answers:

2

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.

+16  A: 

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.

Reto Meier
As of Android 2.2, this doesn't cause a compiler error, but any subdirectories are ignored when generating the R class. This really sucks, and makes it hard to manage larger projects. =/
Nik Reiman
Totally agree. They need to come up with a better solution supporting sub-folders. It can't be that hard.
znq
+1  A: 

Yes - it does suck :) However you can use the assets folder and have sub directories in there and load images that way.

Lemonsanver