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
For it you must to do some steps:
- Create Directory in
res
folder which calledlayouts
. - Create Resource Folder in
layouts
folder which calledactivity
(To create a Resource folder you should choose New -> Folder -> Res Folder, after it write the correct path as examplesrc/main/res/layouts/activity
); - Create Directory in
activity
folder which is calledlayout
. - Do the same for
fragment
andadapter
folders; - 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.
Note: this solution works just for Project perspective.