How to store layouts in different folders in Android project

Android layouts in different folders


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.

CategoriesUncategorized

12 thoughts on “How to store layouts in different folders in Android project

  1. Hi Alex,

    It is very useful article for managing resource file in big project. I have tried it and it works. But I am getting error for actionLayout
    “No resource found that matches the given name (at ‘actionLayout’ with value ‘@layout/menu’).”
    Will appreciate your help 🙂

    1. Hi,

      thank you for your comment.

      I fixed problem with incorrect resource folder. May be it’s a reason.
      Try to change
      ‘src/main/res/layouts/activity’
      to
      ‘src/main/res/layout/activity’.
      And of course the same for other folders with resources.

      Please share your code and I’ll try to help you.

  2. Hi this is really good idea, but unfortunately I am getting “URI is not registered” erron for

    xmlns:app=”http://schemas.android.com/apk/res-auto”
    xmlns:tools=”http://schemas.android.com/tools”

    Can you please help me?

  3. Hi,
    How do you call the layout in the Java class ?

    setContentView(R.layout.?) ?
    This line doesn’t work…

    Could you help me please ?

    1. Please check the required path in the sourceSets section. Afterwards, you can call the setContentView method with layout name:

      setContentView(R.layout.activity_main);
      
  4. What about this? am getting this error have a look at this attached snapshot.

    am just trying to create new res folder “Student_data” in which i will store profile.xml file and then i will call these files in my java file something like this

    setContentView(R.layout.student_data.profile);

    Please check this snapshot

    ===============> http://prntscr.com/hxck9t <=============

Leave a Reply

Your email address will not be published. Required fields are marked *