You can manage your resource’s XML files (layout, drawable,..) better, by grouping them into separate subfolders corresponding to the 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](https://www.howtodoandroid.com/wp-content/uploads/2021/03/switch-to-project-view.png)
Right-click in res, selectNew→Directory and name it as layouts.
![Naming the resource folder as layouts](https://www.howtodoandroid.com/wp-content/uploads/2021/03/Naming-the-resource-folder-as-layouts.png)
Step 2 — Create The Features Folder
Then select the folder you created, Then right-click and select New → Folder → Res Folder.
![Create New Res Folder For The Feature](https://www.howtodoandroid.com/wp-content/uploads/2021/03/Create-New-Res-Folder-For-The-Feature-1.png)
Name the folder based on your feature. In my case, I have named as user, details.
![Naming the resource folder by feature](https://www.howtodoandroid.com/wp-content/uploads/2021/03/Naming-the-resource-folder-by-feature.png)
Step 3 — Adding Resource Folder For The Features
Then, Create resource folders likelayout, drawable, menu,anim, etcfor the created feature folders. In my example, I have created the layout and drawable resource folders.
![Resource Folder For The Features](https://www.howtodoandroid.com/wp-content/uploads/2021/03/Resource-Folder-For-The-Features.png)
Step 4 — Android Resource Folders Into Android Resources
DmytroDanylyk(A Google Developers Expert) says, That we can have multiple res folders within by adding resource sets.
Add the below lines of code to your app’s build.gradle
file inside the 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 and drawable resources directly into respecting feature folder. Then, To Access the resources you can use the standard ways of calling like R.layout.activity_user.xml,R.drawable.map.
![Adding Resources To Features Folder](https://www.howtodoandroid.com/wp-content/uploads/2021/03/Adding-Resources-To-Features-Folder.png)
![Accessing Resource From Activity](https://www.howtodoandroid.com/wp-content/uploads/2021/03/Accessing-Resource-From-Activity.png)
That’s it. Thanks for reading.
Leave a Reply