How to reference colour attribute in drawable?

You might need to do the following to fix your problem:

1) Define 2 colors for each theme in your colors file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="my_color_dark">#ff33B5E5</color>
    <color name="my_color_light">#ff355689</color>
</resources>

2) Create file res/values/attrs.xml with contents:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="my_color" format="reference" />
</resources>

3) Assuming you have 2 themes in your styles.xml (Theme.dark and Theme.light) define:

<style name="Theme.dark" parent="@style/Theme.Sherlock">
    <item name="my_color">@color/my_color_dark</item>
</style>

<style name="Theme.light" parent="@style/Theme.Sherlock.Light">
    <item name="my_color">@color/my_color_light</item>
</style>

4) Use the color in a drawable:

<color android:color="?attr/my_color"/>

Hope this should fix your problem.

Leave a Comment