Android: how to use the current theme's colors in a drawable xml?

user3290180 picture user3290180 · Feb 6, 2016 · Viewed 9.9k times · Source

I have defined two themes with different primary, primary dark, primary light and accent colors.

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>

<style name="AppTheme2" parent="AppTheme">
    <item name="colorPrimary">@color/primary_2</item>
    <item name="colorPrimaryDark">@color/primary_dark_2</item>
    <item name="colorAccent">@color/accent_2</item>
</style>

AppTheme is default for <application/> Then I set AppTheme2 in specific activities.

In a drawable file xml I'm using the same primary colors of AppTheme defined in colors.xml

The drawable is used in many activities from both themes, so when there is AppTheme2 I see the different colors. Is there a way to make the drawable use the colors of the current theme for the current activity?

Answer

e-shfiyut picture e-shfiyut · May 25, 2016

You can use this attribute to get color from currently used theme:

android:color="?colorPrimary"

This is possible only for android 5.0 or higher.