rookiedev
rookiedev

Reputation: 1127

What is the purpose of the file "drawables.xml" under the "values" directory?

For an android studio project, I found a file named "drawables.xml" in the "values" folder

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <item name="ic_menu_camera" type="drawable">@android:drawable/ic_menu_camera</item>
    <item name="ic_menu_gallery" type="drawable">@android:drawable/ic_menu_gallery</item>
    <item name="ic_menu_slideshow" type="drawable">@android:drawable/ic_menu_slideshow</item>
    <item name="ic_menu_manage" type="drawable">@android:drawable/ic_menu_manage</item>
    <item name="ic_menu_share" type="drawable">@android:drawable/ic_menu_share</item>
    <item name="ic_menu_send" type="drawable">@android:drawable/ic_menu_send</item>
</resources>

What's the purpose of this file? Why does it create another reference to an existing drawable and why not just use "@android:drawable/ic_menu_camera"?

Upvotes: 17

Views: 3018

Answers (3)

kenyee
kenyee

Reputation: 2369

This is very useful for whitelabeling. So e.g., your main app can use one drawable but a whitelabel could use another but your code can reference the drawable using a single reference.

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1007658

It may be that the developer had a bit of future-proofing in mind.

The developer could have had Java code reference android.R.drawable.ic_menu_camera and have layouts or other things reference @android:drawable/ic_menu_camera. However, if at some later point, the developer needed to switch from using a platform drawable to a custom one, then all of those references would need to be changed.

The drawable aliases set up in this file allow Java code to reference R.drawable.ic_menu_camera and layouts and other things reference @drawable/ic_menu_camera. Right now, the aliases indicate that those drawables "redirect" to platform drawables. However, at some time in the future, the developer could add a custom ic_menu_camera drawable, remove the alias... and nothing else needs to change in the Java code, layouts, etc.

Few apps refer to platform drawables, and so you will not see this sort of trick used most of the time. But, for cases where apps do refer to platform resources, this aliasing approach can reduce long-term maintenance.

FWIW, I cover this more in this blog post.

Upvotes: 16

Well In my opinion this file is useless because you already have the drawable file names.

Upvotes: 1

Related Questions