Android Layouts

How to store layouts in different folders in Android project

Alex Zhukovich 1 min read
How to store layouts in different folders in Android project
Table of Contents

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 for this problem you can store your layouts in different folders. You can separate your layouts to activity layouts, fragment layouts, adapter layouts, etc.

For instance, you can separate layout like this

Demo: Separate layout folders

For it you must to do some steps:

  1. Create Directory in res folder which called layouts.
  2. Create Resource Folder in layouts folder which called activity (To create a Resource folder you should choose New -> Folder -> Res Folder, after it write the correct path as example src/main/res/layouts/activity);
  3. Create Directory in activity folder which is 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.

Demo: Separate layout folders

Note: this solution works just for Project perspective.


Mobile development with Alex

A blog about Android development & testing, Best Practices, Tips and Tricks

Share

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Mobile development with Alex.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.