Android

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 dev & testing with Alex Zhukovich

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

Share
Comments
More from Mobile dev & testing with Alex Zhukovich
Jetpack Compose: RadioButton
Jetpack Compose

Jetpack Compose: RadioButton

Many mobile apps ask a user to provide data, and RadioButton component can simplify the onboarding process and how users can fill the information. I'll show you the possibilities of "RadioButton", how to customize and test it in JetpackCompose.
Alex Zhukovich 9 min read

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Mobile dev & testing with Alex Zhukovich.

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

Success! Your billing info has been updated.

Your billing was not updated.