Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the same drawable for 2 different SDK versions

I am trying to add support for the new Material UI in my apps, but I have encountered an annoying situation.

Before SDK 14 the black app menu needed white icons. Starting with SDK 14 the Holo light theme needed dark icons. Now when migrating to Material I need white icons once again. So basically I have a folder drawable-hdpi-v11, one drawable-hdpi-v14 and one drawable-hdpi-v21. drawable-hdpi-v11 and drawable-hdpi-v21 have the same images and of course Lint warns me that I have the same resources duplicated.

I have look into aliases Creating alias resources, but it doesn't seem to offer the functionality I need. Do you know any way to obtain the same result (white images for SDK<14 or SDK>=21, dark for SDK>=14 and SDK<21) without duplicating the resources?

like image 907
Adrian-Costin Țundrea Avatar asked Nov 10 '14 19:11

Adrian-Costin Țundrea


1 Answers

You want @drawable/ic_action_heart to resolve to:

  • white on < 14
  • dark on >= 14 and < 21
  • white on >= 21

In that case:

  • Have a dark version of the icon as ic_action_heart.png in res/drawable-hdpi-v14/

  • Have a light version of the icon as ic_action_heart_white.png in res/drawable-hdpi/

  • Have a drawable alias, named ic_action_heart.xml, in res/drawable-hdpi-v21/, pointing to @drawable/ic_action_heart_white

  • Have a drawable alias, named ic_action_heart.xml, in res/drawable-hdpi/, pointing to @drawable/ic_action_heart_white

And, of course, you would have the same basic structure in other density buckets (e.g., -xxhdpi). Since density is more important than is API level, I think you will need to have density-specific versions of the aliases.

like image 72
CommonsWare Avatar answered Nov 02 '22 17:11

CommonsWare