Android: how to use current theme colors in drawing XML?

I defined two themes with different initial, primary dark, primary 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> 

The default AppTheme for <application/> Then I install AppTheme2 in specific actions.

In the drawn xml file, I use the same AppTheme primary colors as in colors.xml

Selectable is used in many actions from both topics, so when there is AppTheme2, I see different colors. Is there a way to make usable the colors of the current theme for the current activity?

+8
android xml runtime xml-drawable android-theme
source share
1 answer

This attribute can be used to get the color from the current theme:

 android:color="?colorPrimary" 

This is only possible in Android version 5.0 or higher.

+16
source share

All Articles