Search code examples
androidandroid-xmlandroid-drawableandroid-selectorandroid-attributes

Use of attr reference in selector StateListDrawable


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>?


Solution

  • 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