3

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?

Joris
  • 1,437
  • 1
  • 15
  • 22

1 Answers1

1

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?

Community
  • 1
  • 1
and_dev
  • 3,723
  • 1
  • 20
  • 28
  • I see, thanks for the link. That issue seems to be about not being able to reference attributes at all in drawables, but for me that's not true since I can reference ?attr/color_control_highlight_default in the ripple resource which works fine... – Joris Jan 20 '16 at 10:54
  • Is it pre-21 or post-21? – and_dev Jan 20 '16 at 11:03
  • This is all for post 21, I have separate resources completely for below 21 – Joris Jan 20 '16 at 11:06
  • Ok, and you are sure that you have the missing drawable exactly in the -v21 folder? – and_dev Jan 20 '16 at 13:15
  • Yes completely sure, also since if I change the attribute references to color and drawable references everything works fine. – Joris Jan 20 '16 at 14:44