Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 834
user3290180 Avatar asked Feb 06 '16 17:02

user3290180


People also ask

How do I change the color of a drawable in xml?

Use app:drawableTint="@color/yourColor" in the xml instade android:drawableTint="@color/yourColor" . It has the backward compatibility.

How do you use theme colors on Android?

Use the colors in your themeOpen themes. xml (app > res > values > themes > themes. xml). Change colorPrimary to the primary color you selected, @color/green .

Where is colors xml in Android Studio?

XML file saved at res/values/colors.


1 Answers

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.

like image 173
e-shfiyut Avatar answered Oct 05 '22 04:10

e-shfiyut