0

I found a lot of posts saying that it's not possible to add subfolders to drawables and other default folders, but all those posts were pretty old, so I'm wondering if it's still not possible to add subfolders.

I found out that you can create folders at the same level as "res" and add them in the gradle file as

sourceSets
{
    main
    {
        res
        {
            srcDir 'src/main/res-mytest'
        }
    }
}

But I tried to add xmls and images in that res-mytest folder and I can't use them as:

<TextView ...
android:background="res-mytest/borders_1.xml">

What am I doing wrong? Thanks.

ND1010_
  • 3,743
  • 24
  • 41
kuonb
  • 198
  • 10
  • Access the resources normally. Do not include the folder name. All resources are treated as though they are in the default folder. – Kuffs Sep 16 '17 at 09:57
  • What do you mean by normally? because normally I would write "@drawable/borders_1" but since it's not in the root directory of "drawable" it doesn't work. – kuonb Sep 16 '17 at 10:02
  • That is how it should work. Any folders you add in this way are simply merged by the build system and treated as though they are all in the same folder. If they are not in your case, you have made a mistake in the configuration. – Kuffs Sep 16 '17 at 11:29
  • So again, I have the following folders now added to the srcDir: 'src/main/res/drawable/folder1/borders', 'src/main/res/drawable/folder1'. I synchronize the build.gradle file. But when I try to access to the files folder1/borders.xml and folder1/borders/borders2.xml I can't. You are telling me that I should be able to access them by simply writing "@drawable/borders2" or "@drawable/borders" right? It doesn't work. – kuonb Sep 16 '17 at 11:52
  • See my comments on the answer below. You cannot have subfolders – Kuffs Sep 16 '17 at 11:55
  • I'm confused because in that picture "layouts" have a subfolder "chat" and "chat" has a subfolder itself. How is that possible? Then where am I supposed to add those folders? at the same level as "res" and "java" or inside "res"? – kuonb Sep 16 '17 at 11:58
  • 1
    Probably because "layouts" isn't a standard resources folder. "layout" is – Kuffs Sep 16 '17 at 12:04
  • See my answer here for how I do it. https://stackoverflow.com/questions/16577782/sub-folders-in-drawable-resource-folder/19859379#19859379 – Kuffs Sep 16 '17 at 12:09
  • Okay, I was not aware that the name of the folders "drawable" has to be maintained. This means that we have to use different names even if they are in different folders which is still very inconvenient. I thought we could create a resource dir anywhere and then access like @myresource/resid – kuonb Sep 16 '17 at 13:13
  • You are adding SETS of resource folders not individual folders. The contents of each set must still follow the standard naming rules. Yes all files must be uniquely named as they are all merged into the same folder at build time. – Kuffs Sep 16 '17 at 13:31

1 Answers1

1

no . it is possible and easy actually .. follow this picture bellow

enter image description here

Amr Alaa
  • 1,156
  • 11
  • 16
  • I tried to follow that. I create a folder within "drawables" called "folder1", and then inside a folder called "borders". I move a specific borders.xml and I got an error on Error: "URI not registered". I also add the srcdirs as srcDir 'src/main/res/drawable/folder1/borders' – kuonb Sep 16 '17 at 09:52
  • You cannot have subfolder in previously defined resource folders. Each resource folder must be flat and have no subfolders as otherwise you we you'll receive the error about Uri not registered. – Kuffs Sep 16 '17 at 11:32
  • Note that this only works for Android Studio or other Gradle-based builds. Other development tools may not support this approach. – CommonsWare Sep 16 '17 at 11:33