Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"?

like image 438
rookiedev Avatar asked Aug 22 '17 22:08

rookiedev


1 Answers

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.

like image 193
CommonsWare Avatar answered Oct 08 '22 17:10

CommonsWare