Joris
Joris

Reputation: 1457

Drawable resources referencing attributes

I have the following problem:

My app has a light and a dark theme and I'm trying to apply touch feedback to some custom views for both above and below v21 (ripple touch feedback). I've created two drawable resources in drawable-v21 for a toggle button, one for light and one for dark:

Dark:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/color_control_highlight_default">
    <item android:id="@android:id/mask" android:drawable="@drawable/dark_button_border"/>
    <item android:drawable="@drawable/button_toggle_states"/>
</ripple>

Light:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/color_control_highlight_default">
    <item android:id="@android:id/mask" android:drawable="@drawable/light_button_border" />
    <item android:drawable="@drawable/button_toggle_states" />
</ripple>

As can be seen both resources reference another drawable which defines the colours for different states of the buttons:

button_toggle_states:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="?attr/color_primary_1" android:state_focused="true" />
    <item android:drawable="?attr/color_primary_1" android:state_checked="true" />
    <item android:drawable="?attr/color_primary_1" android:state_selected="true" />
    <item android:drawable="?attr/button_border" />
</selector>

This drawable references attributes that change between the light and the dark theme and this is where my problem is. When trying to use this drawable I get a runtime error that the button_toggle_states in res/drawable-v21 can't be found. When I change the drawable to reference colours and other drawables instead of attributes, i.e.

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/lt_blue" android:state_focused="true" />
    <item android:drawable="@color/lt_blue" android:state_checked="true" />
    <item android:drawable="@color/lt_blue" android:state_selected="true" />
    <item android:drawable="@drawable/light_button_border" />
</selector>

everything works fine. But with that solution I'll have to make two button_toggle_state files, one for dark and one for light theme, while I thought this shouldn't be needed since we can reference attributes in v21 and up.

Is there somethin I'm doing wrong or is referencing attributes in a drawable that is used by another drawable just not supported?

Upvotes: 3

Views: 1983

Answers (1)

and_dev
and_dev

Reputation: 3861

i ran into the same issue and was not able to get this running. My workaround is to have the drawable itself as a reference in the theme and then have two Drawables: one for light and one for dark.

See also: https://code.google.com/p/android/issues/detail?id=26251

How to reference colour attribute in drawable?

Upvotes: 1

Related Questions