Search code examples
androidlayoutgroupingsubdirectory

Grouping layout by folders (Android)


With the growth of the project in the folder layouts accumulated a large number of files with markup. This naturally causes some inconvenience.

Can they be grouped into subfolders in any way?


Solution

  • You can follow these steps one by one:

    1. Create Directory in folder res called "layouts".
    2. Create Resource Folder in folder named layouts, lets say call it "main-activity"
    3. Create Directory in folder named activity called "layout"
    4. Move your files to this folder that makes most sense.
    5. Repeat for all your files.

    At last add this to your build.gradle which declares where all the resources are located at:

    android {
        ...
        defaultConfig {
            ...
        }
        buildTypes {
            ...
        }
        sourceSets {
            main {
                manifest.srcFile 'src/main/AndroidManifest.xml'
                java.srcDirs = ['src/main/java', 'apt_generated']
                aidl.srcDirs = ['src/main.aidl', 'apt_generated']
                assets.srcDirs = ['src/main/assets']
                res.srcDirs =
                        [
                                'src/main/res/layout/main-activity',
                                'src/main/res/layout/adapter',
                                'src/main/res'
                        ]
            }
        }
    }
    

    Notice! src/main/res/layouts MUST be second last and src/main/res MUST be the last path

    Also, switch to Project perspective to work.