Layouts in different folders in Android App
Your ‘res\layout’ folder is too big? Try making sub-folders.
Normally we store every xml layout file inside the res/layout
folder. It is feasible and simple to manage small projects. But in a large and heavy project, it is very common that the resources folder grows and grows and if your project have more then 35 layouts it’s a problem to find a required layout. As solution of this problem we can store your layouts in different folders. As example you can separate your layouts to “activity layouts”, “fragment layouts”, “adapter layouts”, etc. You can separate layout like this:
Configure Multiple Layout Folder
- Switch to Project view;
- Copy all of the XML files out of your layout directory, and put them into a directory on the desktop or something for backup. (present under android/app/src/main/res/layout);
- Delete the entire ‘layout’ directory (Make sure you backed everything up from step 1!!!);
- Right click the ‘res’ directory and select new > directory to create Directory in folder res which called “layouts” (I prefer calling it ‘layouts’ but this can be whatever you want, but it will not be a ‘fragment’ directory or ‘activity’ directory, that comes later).;
- Right click the new “layouts” directory and select new > directory. (This will be the name of the type of XML files you will have in it, for example, ‘fragments’, ‘activities’ or ‘adapters’ etc.);
- Right click the ‘activities’ directory and select new > directory once again and name this directory “layout”. (Note: This MUST be named ‘layout’ !!! very important);
- Do the same for fragment and adapter folders;
- Put the XML files you want inside the new ‘layout’ directory from the backup you made on your desktop;
- Now 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).[ If you make another directory like ‘activities’ or ‘fragments’ you must add them here inres.srcDirs
manually];
sourceSets {
main {
res.srcDirs =
[
'src/main/res/layouts/activities',
'src/main/res/layouts/fragments,
'src/main/res/layouts/adapters,
'src/main/res/layouts',
'src/main/res'
]
}
}
7. Sync your project and everything will work correctly;
If you want to use different layout mode like portrait mode, landscape mode, etc. You have to create folder like this:
Unfortunately this solution works only for “Project” view;
IMPORTANT NOTE:
- Change the name of feature directory (‘activities’, ‘fragmetns’) as per your project.
- Add all feature directory path in
sourceSets
in build.gradle in the same way. src/main/res/layouts
MUST be second last andsrc/main/res
MUST be the last path.
THERE IS A MAGIC !
If you want to improve it on a bit, then just change the sourceSets
in gradle setting like this:
sourceSets {
main {
res.srcDirs = [
file("src/main/res/layouts/").listFiles(),
"src/main/res/layouts",
"src/main/res"
]
}
}
Now if you add more folders and layouts, you don’t need to append a long list of source folders here, gradle will get all the folders for you !!!
Congratulations ! You have successfully created subdirectories in your layouts folder. Be sure to give claps if you find this article to be helpful. Thanks for reading.
Do you know it is very easy to decompile an Android App and get the API you used in your project???? To secure your API Keys you can see this article Securing API Keys in Android App
Always try to use PROGUARD in your Android app to keep your codes secured. If you have no idea on Proguard then you can see this article Secure Your Codes by Enabling PROGUARD in you Anroid App.
Happy Coding :)