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.

12 CommentsClose Comments

12 Comments

  • nitin
    Posted November 9, 2015 at 12:10 pm 0Likes

    Hi..
    Thanks, is very useful in big project.

  • kinsleykajiva
    Posted June 26, 2016 at 2:10 pm 0Likes

    thank you for this tutorial please keep on doing these tutorials

  • Preeti
    Posted December 2, 2016 at 7:04 am 0Likes

    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 🙂

    • Posted December 9, 2016 at 10:44 pm 0Likes

      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.

  • arvind
    Posted July 6, 2017 at 11:21 am 0Likes

    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?

    • Posted November 23, 2017 at 8:08 pm 0Likes

      Could you provide more information, please

  • Kaniar Kode
    Posted September 12, 2017 at 10:49 am 0Likes

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

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

    Could you help me please ?

    • Posted November 23, 2017 at 8:09 pm 0Likes

      It should work, but I need more info for helping you.

  • Muthu kumar
    Posted December 12, 2017 at 5:27 am 0Likes

    Hi, I have error in this line. how to layout file find out setContentView?

    setContentView(R.layout..?)

    • Posted January 16, 2018 at 6:56 pm 0Likes

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

      setContentView(R.layout.activity_main);
      
  • Rohit Kumar
    Posted January 7, 2018 at 9:11 pm 0Likes

    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 <=============

    • Posted January 16, 2018 at 7:15 pm 0Likes

      In this case, you use

      setContentView(R.layout.profile);
      

Leave a comment