Layouts in different folders in Android App

Your ‘res\layout’ folder is too big? Try making sub-folders.

Developer Shishir
Aug 31, 2018 · 3 min read
Fig: Multiple layout folders
  1. Copy all of the XML files out of your layout directory, and put them into a directory on the desktop or something for backup. (present under android/app/src/main/res/layout);
  2. Delete the entire ‘layout’ directory (Make sure you backed everything up from step 1!!!);
  3. Right click the ‘res’ directory and select new > directory to create Directory in folder res which called “layouts” (I prefer calling it ‘layouts’ but this can be whatever you want, but it will not be a ‘fragment’ directory or ‘activity’ directory, that comes later).;
  4. Right click the new “layouts” directory and select new > directory. (This will be the name of the type of XML files you will have in it, for example, ‘fragments’, ‘activities’ or ‘adapters’ etc.);
  5. Right click the ‘activities’ directory and select new > directory once again and name this directory “layout”. (Note: This MUST be named ‘layout’ !!! very important);
  6. Do the same for fragment and adapter folders;
  7. Put the XML files you want inside the new ‘layout’ directory from the backup you made on your desktop;
  8. Now go into your modules gradle.build file and create a sourceSets definition like this…(Make sure ‘src/main/res/layouts’ & ‘src/main/res’ are always the bottom two!!!! Like I am showing below).[ If you make another directory like ‘activities’ or ‘fragments’ you must add them here in res.srcDirs manually];
sourceSets {
main {
res.srcDirs =
[
'src/main/res/layouts/activities',
'src/main/res/layouts/fragments,
'src/main/res/layouts/adapters,
'src/main/res/layouts',
'src/main/res'
]
}
}
Fig: Layout folder for landscape mode
  1. Add all feature directory path in sourceSets in build.gradle in the same way.
  2. src/main/res/layouts MUST be second last and src/main/res MUST be the last path.

THERE IS A MAGIC !

If you want to improve it on a bit, then just change the sourceSets in gradle setting like this:

sourceSets {
main {
res.srcDirs = [
file("src/main/res/layouts/").listFiles(),
"src/main/res/layouts",
"src/main/res"
]
}
}

Programming Lite

Here i share article on Java, Android, Swift, iOS

Developer Shishir

Written by

{ 'designation' : 'Software Developer' , 'hobby' : [ 'Music', 'Photography', 'Travelling' ] ,’email’: ‘shishirthedev@gmail.com’ }

Programming Lite

Here i share article on Java, Android, Swift, iOS