How to put android layout files in subfolders

As your app grows, putting things under subfolders can save a lot of time

So you started developing an android app. Great! And it started growing slowly and steadily. Before you know it, you have many classes and layouts. While arranging classes in subfolders is easy-peasy, doing so for layouts is not straightforward. But if you do so, it makes a huge difference in managing layouts. I’m going to cut to the chase and share with you how can you do so.

1. Switch to Project view

2. Backup your layout files

(present under android/app/src/main/res/layout)

3. Delete layout directory

(same directory mentioned in step 2: android/app/src/main/res/layout)

4. Right click the res directory and select New > select Directory

5. Give any name

(I prefer calling it layouts — because it is parent folder to hold all layouts of our project)

6. Right click the layouts directory and select New > select Directory

7. Name the directory.

You have 2 choices now. Either name by feature (like result_display, user_onboarding, etc). Or name by category (activities, fragments, items, etc). I prefer the former one because by following naming convention (activity_result_display, activity_user_onboarding, etc), you can arrange layout files by default, thus this entire exercise will become kind of useless.

8. Repeat steps 6 and 7 till you have sub-directories for all your layout files

9. Right click one feature directory and select New > select Directory. MOST IMPORTANT: You need to name this directory layout

10. Repeat step 9 for all feature directories

11. Put all corresponding layout files from the backup (created in step 2) to their respective feature directories (under layout folder)

12. Open app build.gradle file and add the following code

i.e. the following code:

sourceSets {
main {
res.srcDirs =
[
'src/main/res/layouts/layout_select_operator',
'src/main/res/layouts',
'src/main/res'
]
}
}

IMPORTANT NOTE:

  1. Please change the name of feature directory (i.e., layout_select_operator in the example given above) as per your project
  2. Add all feature directory path in the same way
  3. Notice the order of adding paths. src/main/res/layouts MUST be second last and src/main/res MUST be the last path

13. Sync project with gradle files

That’s it!

Hope this helps in some small way to maintain a huge code base, if you have one. I believe this should be beneficial regardless of project size as well because it gives a structure to your code base.

Happy coding!

Check out all the top articles at blog.mindorks.com

MindOrks

Our community publishes stories worth reading on Android Development

Mayank Mohan Upadhyay

Written by

Google Certified Mobile Engineer • Sr Mobile Er at Einsite • I am an artist who engineers software

MindOrks

MindOrks

Our community publishes stories worth reading on Android Development

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade