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 heavy projects, then there should be a hierarchy and sub-folders needed inside the layout folder.
for e.g.
layout
-- layout_personal
-- personal_detail.xml
-- personal_other.xml
--layout_address
-- address1.xml
-- address2.xml
Like the same way, we would like to have sub-folders for the large application, so is there any way to do so inside the android project?
I am able to create layout-personal and layout_address sub-folders inside the layout folder, but when the time comes to access the xml layout file using R.layout._______ , at that time there is no any xml layout pop-up inside the menu.
You CAN do this with gradle. I've made a demo project showing how.
The trick is to use gradle's ability to merge multiple resource folders, and set the res folder as well as the nested subfolders in the sourceSets block.
The quirk is that you can't declare a container resource folder before you declare that folder's child resource folders.
Below is the sourceSets block from the build.gradle file from the demo. Notice that the subfolders are declared first.
Also, the direct parent of your actual resource files (pngs, xml layouts, etc..) does still need to correspond with the specification.
If you use the method in the approved answer, and want to improve it on a bit, then change the gradle setting like this:
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.
Well, the short answer is no. But you definitely can have multiple
res
folders. That, I think, is as close as you can get to having subfolders for thelayout
folder. Here's how you do it.Now with Android Studio and Gradle, you can have multiple resource folders in your project. Allowing to organize not only your layout files but any kind of resources.
It's not exactly a sub-folder, but may separte parts of your application.
The configuration is like this:
Check the documentation.
The top answer by @eski is good, but the code is not elegant to use, so I wrote a groovy script in gradle for general use. It's applied to all build type and product flavor and not only can be use for layout, you can also add subfolder for any other resources type such as drawable. Here is the code(put it in
android
block of project-level gradle file):How to do in practice?
For example, I want to create subfolder named
activity
forlayout
, add a string by any name inresDirs
variable such aslayouts
, then the layout xml file should be put inres\layouts\activity\layout\xxx.xml
.If I want to create subfolder named
selectors
fordrawable
, add a string by any name inresDirs
variable such asdrawables
, then the drawable xml file should be put inres\drawables\selectors\drawable\xxx.xml
.The folder name such as
layouts
anddrawables
is defined inresDirs
variable, it can be any string. All subfolder created by you such asactivity
orselectors
are regarded as the same asres
folder. So inselectors
folder, we must createdrawable
folder additionally and put xml files indrawable
folder, after that gradle can recognize the xml files as drawable normally.Now we can easily do with JetBrains plugin called "Android File Grouping"
check out this link
Android File Grouping