13

In my application I have several folders and subfolders of images. They are inside drawables. How can I retrieve images from drawables subfolders?

Example:

drawable>actions>sports>soccer.png

How can I get this "soccer.png" photo?

Any help is appreciated.

Thanks!

user2864740
  • 60,010
  • 15
  • 145
  • 220
Carlos Pereira
  • 1,914
  • 6
  • 24
  • 35

2 Answers2

18

This is now (sort of) possible by using Android Studio and Gradle.

Whilst subfolders are still not possible, it is possible to separate resources into different sets and have them merged by the build system.

As an example, to simplify my project, I wanted to keep theme specific drawables separate from standard drawables.

I created a new resource folder named 'res_lighttheme' as illustrated in the picture below.

Folder Structure

I then added a pointer to this folder to my build.gradle file like this. To add more than one just add another line to the definition.

Build Gradle

The result is that BOTH folders are then considered to be valid destinations for resources. A build error will be generated if there is a conflict. i.e if the same resource is included in both folders.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • the inner line is not correct. It mus be: srcDirs = ['src/main/res', 'src/main/your_res_folder'] and must include your default res folder – Chris623 Nov 11 '15 at 15:25
  • 1
    Not necessarily. Your solution replaces all source directories. Mine adds new ones to any that already exist (such as the default one). Neither method is wrong but my solution is perfectly valid and works. – Kuffs Nov 11 '15 at 15:28
  • your method is deprecated, it throws a gradle error so it must be srcDirs from now on. – Chris623 Nov 11 '15 at 15:31
  • 2
    It still works perfectly fine in my projects but the answer is 2 years old so to say it is incorrect is a little picky. Also, there is no mention of deprecation in the docs which still give an example using srcDir.: https://docs.gradle.org/current/userguide/java_plugin.html – Kuffs Nov 11 '15 at 15:39
  • Method does not deprecated. It works perfectly. Also, this works too: res.srcDirs += 'src/main/res_myotherdir' – Sinan Ceylan Apr 25 '19 at 02:33
16

No, the android resources mechanism doesn't support subfolders in the drawable directory, you can't put it.

I think if you 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.

The only one thing is put the images in flat names like, drawable_actions_sports_soccer.png.

user370305
  • 108,599
  • 23
  • 164
  • 151