3

So I'm trying to make a selector that uses a theme-based color as its background. Following the instructions from this SO answer, I first define my color drawable in res/values/colors.xml:

<color name="selected">#FFF7C9</color>

Then I define an attribute in res/values/attrs.xml:

<attr name="drawable_selected" format="reference" />

and then in my theme, I set attribute to my color drawable (res/values/styles.xml):

<style name="AppThemeWhite" parent="AppTheme">
    <item name="drawable_selected">@color/selected</item>
</style>

finally, I reference 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 trying to inflate the class that uses the selector. However, when I change the selector's state_activated to reference the drawable directly using 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 drawable to 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 causes a crash as well! And changing the color drawable back to a hardcoded value of #FFF7C9 fixes it. It seems this whole theming system is pretty broken...

Community
  • 1
  • 1
woojoo666
  • 7,801
  • 7
  • 45
  • 57

1 Answers1

2

Reason

Yeah, referencing custom theme attributes from drawable (or colors) doesn't work on Android currently.

Here you can see more details about the issue that was reported long time ago: https://code.google.com/p/android/issues/detail?id=26251

As you can see they have finally resolved it in Android L release, but anywhere lower than L such referencing will fail.

Solution

To work around this issue you need to do something like that:

<selector>
    <item android:state_activated="true" android:drawable="@color/selected" />
    <item android:drawable="@android:color/transparent" />
</selector>

where @color/selected is defined as at the beginning of your post:

<color name="selected">#FFF7C9</color>
Maciej Ciemięga
  • 10,125
  • 1
  • 41
  • 48
  • I tried this in L preview though...maybe only in the official release? – woojoo666 Sep 16 '14 at 06:05
  • I cannot test it myself now so I can only see what is said on their issue tracker. But it doesn't matter if it's fixed or not - you probably don't want to relay on it (unless you want to target your app on L only). Please just use the workaround. – Maciej Ciemięga Sep 16 '14 at 06:46
  • Yea I already used this workaround, I was just asking to see if anybody else was facing the same issues, and if I should ask to reopen the issue report. Thanks for all the help tho! – woojoo666 Sep 16 '14 at 08:20
  • @woojoo666 If you want reopen that issue you should test it on L-preview with the project provided in second comment. I have proposed the solution because you didn't mention that you already use this workaround. Btw. What answer to this question you are looking for? I've confirmed you that this is a known issue, provided related issue in Android bug tracker and proposed working workaround. Did I misunderstand the question at any point? Please tell me so I could update my answer if it's not appropriate. – Maciej Ciemięga Sep 16 '14 at 08:35
  • Sorry I was still researching the issue, I'll mark this as the answer now lol – woojoo666 Sep 16 '14 at 08:40
  • Ah OK, I just thought that you want some more details or anything like that - so just asked:) – Maciej Ciemięga Sep 16 '14 at 08:43
  • I do not understand the solution mentioned in the post. It isn't solution or workaround to support theme based colour referencing. However, the reason mentioned does have supporting facts. – hjchin Jul 31 '19 at 15:10