Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use sub folders in drawables in Android? [duplicate]

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!

like image 648
Carlos Pereira Avatar asked Jan 27 '12 04:01

Carlos Pereira


People also ask

Can The Android drawable directory contain subdirectories?

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.

What folder is Drawables directory in?

In Android Studio inside the res folder, one can find the drawable folder, layout folder, mipmap folder, values folder, etc. Among them, the drawable folder contains the different types of images used for the development of the application.

How do you add Drawables?

Drag and drop your images directly onto the Resource Manager window in Android Studio. Alternatively, you can click the plus icon (+), choose Import Drawables, as shown in figure 3, and then select the files and folders that you want to import. Figure 3: Select Import Drawables from the dropdown menu.


2 Answers

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.

like image 160
Kuffs Avatar answered Oct 05 '22 11:10

Kuffs


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.

like image 22
user370305 Avatar answered Oct 05 '22 11:10

user370305