When I started to create the Goal Striver app over the summer with my friend, Harrison Lee the CTO of Goal Striver, we realized that near the end of the project deadline, there was over 20 layout files and it is annoying to deal with. I looked over the internet and found little results that talk about this matter.

Fast forward 15 months later, I decided to look into the topic again and found two posts: one on StackOverflow and on Google Plus. It was kind of hard to find the information, so I thought I would make a tutorial post about this topic in hopes of answering those who were once in my shoes the question of how to create subdirectories in android studio layouts folder.

To make subdirectories in the layouts folder, it is easier to deal with when you are starting off with a new project since there are almost no layouts to deal with when moving files around.

The Process of Creating Subdirectories in the Layouts Folder

First, you want to make a backup of all the xml layouts in your 'layout' folder. Once you have done that, delete the 'layout' folder from your Android projects folder. In the 'res' folder, there shouldn't be any 'layout' folder.

Next, you want to right click the res folder, New > Directory, and name it 'layouts' (note the plural form of the word 'layout').

Once you've done that, right click the newly created layouts folder and start creating the new subdirectories that you wish to have in your layouts folder.

directories-show-up-FIN

NOTE - Be sure to change your side panel view of your file structure from the default 'Android' layout into the 'Project' layout from the drop down menu, otherwise you will not be able to see the newly created layouts folder and the subdirectories. The Project layout basically mimics what you see in your file explorer so all files will be shown.

If things look weird in the side panel window (such as the subdirectories being appended to the layouts folder name), the next step you must do is go to your gradle project file and add in the following code with all the directories that we made. This will tell Android Studio where these resources are located at and to combine them upon the creation of the app.

sourceSets {
        main {
            res.srcDirs =
                    [
                            //MOBILE PHONE LAYOUTS
                            'src/main/res/layouts/activities',
                            'src/main/res/layouts/fragments',
                            //10 Inch+ TABLET LAYOUTS
                            'src/main/res/layouts-xlarge',
                            'src/main/res/layouts-xlarge/activities',
                            'src/main/res/layouts-xlarge/fragments',
                            //7 Inch+ TABLET LAYOUTS
                            'src/main/res/layouts-large',
                            'src/main/res/layouts-large/activities',
                            'src/main/res/layouts-large/fragments',
                            'src/main/res/layouts',
                            'src/main/res'
                    ]
        }
    }

gradle-order-matters-FIN

NOTE - In the gradle file, make sure the 'src/main/res' and 'src/main/res/layouts' paths are the last two lines within the res.srcDirs array. The reason you want them to be the last two elements is because when you try to create your first new xml layout file in the newly created subdirectories, Android Studio will generate the old 'layout' folder and add your xml into that folder, and not into the desired subdirectory. By putting the two lines last, it will prevent this scenario from happening (which is what we want).

Once you have finished creating all of your subdirectories, be sure to create a layout directory inside each of the subdirectories you have just created. This is a very important step!

Finally, sync the gradle and your project if needed by pressing that refresh icon button.

Congratulations! You have successfully created subdirectories in your layouts folder.

If you found this post to be helpful, be sure to share it with others who might find this helpful. Also, be sure to subscribe to my newsletter to stay informed of future helpful posts like this one. Thank you for reading!