Right now, I\'m storing every XML layout file inside the \'res/layout\' folder, so it is feasible and simple to manage small projects, but when there is a case of large and
I think the most elegant solution to this problem (given that subfolders are not allowed) is to prepend the file names with the name of the folder you would have placed it inside of. For example, if you have a bunch of layouts for an Activity, Fragment, or just general view called "places" then you should just prepend it with places_my_layout_name. At least this solves the problem of organizing them in a way that they are easier to find within the IDE. It's not the most awesome solution, but it's better than nothing.
I just wanted to add onto eskis' fantastic answer for people having trouble. (Note: This will only work and look like separate directories inside the 'project' view, not the 'android' view unfortunately.)
Tested with the following. BuildToolsVersion = 23.0.0 gradle 1.2.3 & 1.3.0
This is how I got mine to work with an already built project.
Once this is complete, go into your modules gradle.build file and create a sourceSets definition like this...(Make sure 'src/main/res/layouts' & 'src/main/res' are always the bottom two!!!! Like I am showing below).
sourceSets {
main {
res.srcDirs =
[
'src/main/res/layouts/activities',
'src/main/res/layouts/fragments',
'src/main/res/layouts/content',
'src/main/res/layouts',
'src/main/res'
]
}
}
Profit $$$$
But seriously.. this is how I got it to work. Let me know if anyone has any questions.. I can try to help.
Pictures are worth more than words.
The answer is no.
I would like to draw your attention towards this book Pro Android 2 that states:
It is also worth noting a few constraints regarding resources. 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).
Second, there are some similarities between the assets folder and the raw folder under res. Both folders can contain raw files, but the files within raw are considered resources and the files within assets are not.
Note that because the contents of the assets folder are not considered resources, you can put an arbitrary hierarchy of folders and files within it.
Check Bash Flatten Folder script that converts folder hierarchy to a single folder
A way i did it was to create a separate res folder at the same level as the actual res folder in your project, then you can use this in your apps build.gradle
android {
//other stuff
sourceSets {
main.res.srcDirs = ['src/main/res', file('src/main/layouts').listFiles()]
}
}
then each subfolder of your new res folder can be something relating to each particular screen or something in your app, and each folder will have their own layout
/ drawable
/ values
etc keeping things organised and you dont have to update the gradle file manually like some of these other answers require (Just sync your gradle each time you add a new resource folder so it knows about it, and make sure to add the relevant subfolders before adding your xml files).
If you use the method in the approved answer, and want to improve it on a bit, then change the gradle setting like this:
sourceSets {
main {
res.srcDirs = [
file("src/main/res/layouts/").listFiles(),
"src/main/res/layouts",
"src/main/res"
]
}
}
So if you add more folders and layouts, you don't need to come back here and append a long list of source folders, let gradle get all the folders for you.