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:
- Please change the name of feature directory (i.e.,
layout_select_operatorin the example given above) as per your project
- Add all feature directory path in the same way
- Notice the order of adding paths.
src/main/res/layoutsMUST be second last and
src/main/resMUST be the last path
13. Sync project with gradle files
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.