Underline Android Theme not working with Color Drawable (bug?)
So I am trying to create selector
one that uses a background color as the background. Following the instructions in this SO answer , I first define my color in res / values /colors.xml:
<color name="selected">#FFF7C9</color>
Then I define the attribute in res / values /attrs.xml:
<attr name="drawable_selected" format="reference" />
and then in my theme, I set an attribute for my color (res / values / styles.xml):
<style name="AppThemeWhite" parent="AppTheme">
<item name="drawable_selected">@color/selected</item>
</style>
Finally, I am referencing the attribute in my selector (res / drawable / selected_background):
<selector>
<item android:state_activated="true" android:drawable="?drawable_selected" />
<item android:drawable="@android:color/transparent" />
</selector>
When I run this, I get an error Binary XML file line #2: Error inflating class <unknown>
when I try to inflate a class that uses a selector. However, when I change the selector state_activated
to reference the drawable using directly android:drawable="@color/selected"
, it works.
Is this a bug or am I doing something wrong?
EDIT
If I add a color attribute (res / values /attrs.xml)
<attr name="selected_color" format="color" />
and define the color in my theme (res / values /styles.xml)
<item name="selected_color">#FFF7C9</item>
I can change the color usable for change based on them (res / values /colors.xml)
<color name="selected">?selected_color</color>
and reference the drawable directly using android:drawable="@color/selected
in my selector. However, this also crashes! And changing the color returned by the hardcoded value #FFF7C9
fixes it. It seems like this whole theme system is pretty broken ...
source to share
Cause
Yes, references to custom theme attributes from drawable (or colors) don't work on Android at this time.
You can see more details about an issue that was reported long ago here: https://code.google.com/p/android/issues/detail?id=26251
As you can see, they finally resolved it in the Android L release, but somewhere below L, such a link failed.
Decision
To work around this problem, you need to do something like this:
<selector>
<item android:state_activated="true" android:drawable="@color/selected" />
<item android:drawable="@android:color/transparent" />
</selector>
where is @color/selected
defined as at the beginning of your post:
<color name="selected">#FFF7C9</color>
source to share