1

I'm trying to reference ?attr/selectableItemBackgroundBorderless within an XML definition of a StateListDrawable to change the behavior of some of the states defined within the original:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/transparent" android:state_pressed="false" />
    <item android:drawable="?selectableItemBackgroundBorderless" />
</selector>

But this results in a crash:

Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 0: TypedValue{t=0x2/d=0x7f040298 a=-1}
  at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:946)
  at android.content.res.TypedArray.getDrawable(TypedArray.java:930)
  at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:177)
  at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:122)
  at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:142)
  at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1332)
  at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1291)
  at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:833)
    ... 46 more

What I don't understand is, why it actually works with a LayerDrawable which seems to be doing just the same code-wise:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/transparent" />
    <item android:drawable="?selectableItemBackgroundBorderless" />
</layer-list>

Is it possible to use a ?attr/ within a <selector>?

tynn
  • 38,113
  • 8
  • 108
  • 143

3 Answers3

2

It is not possible to resolve any references in selectors. That's why we ended having all the files duplicated (we had the same issue with colors and theming).

You might want to have a look at those closed questions:

  1. https://stackoverflow.com/a/30757188/4310905
  2. Use attributes in selector - Android
and_dev
  • 3,723
  • 1
  • 20
  • 28
0

LayerDrawable flexibility on the use of either a ?attr or a drawable can be misleading. If you use the ?attr, it will try to set a custom theme attribute, but if you use a regular resource it will try to load a drawable entirely. Quoting from the LayerDrawable docs :

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

StateListDrawables only expect drawables to be references as such:

@[package:]drawable/filename

Notice how ?attr or type is omitted.

Reference : https://developer.android.com/guide/topics/resources/drawable-resource

ehanoc
  • 2,187
  • 18
  • 23
0

Just define two of your selectors: e.g. selector_1.xml and selector_2.xml And use them in your styles.xml:

<style ...>
<item name="themeChannelBackground">@drawable/selector_1</item>
</style>

<style ...>
<item name="themeChannelBackground">@drawable/selector_2</item>
</style>
Zar
  • 595
  • 4
  • 17