Mobile development

Alex Zhukovich

How to store layouts in different folders in Android project


Today I’m talking about storing layouts in Android application in different folders. Because sometimes it can be a problem store many layouts in one folder. If your project have more then 30 layouts it’s a problem find a required layout. As solution of this problem you can store your layouts in different folders. As example you can separate your layouts to “activity layouts”, “fragment layouts”, “adapter layouts”, etc.

As example you can separate layout like this
separate layouts - folder

For it you must to do some steps.

  1. Create Directory in folder res which called “layouts”;
  2. Create Resource Folder in folder layouts which called “activity” (For create a Resource folder you must choose “New -> Folder -> Res Folder”, after it write the correct path as example “src/main/res/layouts/activity”);
  3. Create Directory in folder activity which called “layout”;
  4. Do the same for fragment and adapter folders;
  5. Move your layouts for new folders.

Last step use must update build.gradle file for your project.

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    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/activity',
                            'src/main/res/layout/fragment',
                            'src/main/res/layout/dialog',
                            'src/main/res/layout/adapter',
                            'src/main/res'
                    ]
        }
    }
}

After compile your project all link will works correctly.

If you want to use different layout mode, as example portrait mode, landscape mode, etc. You need create folders like this.
different_landscape_modes

Unfortunately this solution works just for “Project” perspective.

Share Share on Reddit0Share on VKTweet about this on TwitterShare on LinkedIn39Share on Google+0Share on Facebook9Flattr the authorEmail this to someoneShare on Tumblr0
« »

© 2017 Mobile development. Theme by Anders Norén.