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.
If you are developing on a linux or a mac box, a workaround would be, to create subfolders which include symbolic links to your layoutfiles. Just use the ln command with -s
The Problem with this is, that your Layout folder still contains all the .xml files. But you could although select them by using the sub-folders. It's the closest thing, to what you would like to have.
I just read, that this might work with Windows, too if you are using Vista or later. There is this
mklink
command. Just google it, have never used it myself.Another problem is, if you have the file opened and try to open it again out the plugin throws a NULL Pointer Exception. But it does not hang up.
Small Problem
I am able to achieve subfolders by following the top answer to this question.
However, as the project grows bigger, you will have many sub-folders:
Not a big problem, but:
app/build.gradle
everytime you add a new folder.Improvement
So I wrote a simple Groovy method to grab all nested folders:
Paste this method outside of the
android {...}
block in yourapp/build.gradle
.How to use
For a structure like this:
Use it like this:
If you have a structure like this:
You will use it like this:
Explanation
getLayoutList()
takesa relative path
as an argument. Therelative path
is relative to the root of the project. So when we input"app/src/main/res/layouts/"
, it will return all the subfolders' name as an array, which will be exactly the same as:Here's the script with comments for understanding:
Hope it helps!
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).
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.
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
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).I use Android File Grouping plugin for Android Studio.It doesn't really allows you to create sub-folders, but it can DISPLAY your files and resources AS they are in different folders. And this is exactly what I wanted.
You can install "Android File Grouping" plugin by
Windows:
Mac:
For Mac, I was able to test it and was not able to search for the plugin. So I downloaded the plugin from here and used the
Install plugin from disk
option from the above setting.Top answers have several disadvantages: you have to add new layout paths, AS places new resources to
res\layouts
folder instead ofres\values
.Combining several answers I wrote similar:
I made folders with this article: http://alexzh.com/tutorials/how-to-store-layouts-in-different-folders-in-android-project/. In order to create subfolders you should use this menu: New > Folder > Res Folder.
UPDATE
After a couple of weeks I found that changes in resources are not noticed by Android Studio. So, some weird bugs appear. For instance, layouts continue to show old sizes, margins. Sometimes AS doesn't find new XML-files (especially during run-time). Sometimes it mixes
view id
s (references to another XML-file). It's often required to pressBuild > Clean Project
orBuild > Rebuild Project
. Read Rebuild required after changing xml layout files in Android Studio.