Multiple Resource Folders In Android

Image for post
Image for post

You can manage your resources XML files (layout, drawable,..) better, by grouping them into separate subfolders corresponding to app’s features.

Step to Create Multiple Resource Folders in Android

  • Create a parent folder that contains all layouts of our project.
  • Create The Features Folder.
  • Adding Resource Folder For The Features.
  • Android Resource Folders Into Android Resources.
  • Adding / Accessing Resource Files.

Step 1 — Create a parent folder that contains all layouts of our project.

Switch project tree explorer to Project view.

Switching To Project View
Switching To Project View

Right click in res,select New Directory, name it as layouts.

Image for post
Image for post

Step 2 — Create The Features Folder

Select folder you created, Then right click and select New → Folder → Res Folder.

Image for post
Image for post

Name the folder based on your feature. In my case, I have named as user, details.

Image for post
Image for post

Step 3 — Adding Resource Folder For The Features

Then, Create resource folders like layout, drawable, menu,anim,etc for the created feature folders. In my example, I have created layout and drawable resource folders.

Image for post
Image for post

Step 4 — Android Resource Folders Into Android Resources

Dmytro Danylyk (A Google Developers Experts) says, That we can have multiple res folders within by add resource sets.

Add the below lines of code into your app’s build.gradle file inside android tag.Then, re-sync Gradle and rebuild the project.

android {
...
sourceSets {
main {
res.srcDirs = [
'src/main/res',
'src/main/res/layouts/user',
'src/main/res/layouts/details']
}
}
}

Step 5 — Adding / Accessing Resource Files

Then, you can add your layout, drawable resources directly into respecting feature folder. Then, To Access the resources you can use standard way of calling like R.layout.activity_user.xml , R.drawable.map.

Image for post
Image for post
Image for post
Image for post

In Conclusion

I believe, It will help you to manage your android project resource better. Thanks for the reading.

Originally published at https://velmm.com on December 21, 2019.

Written by

Learn Android By Doing | howtodoandroid.com | android developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store