Monday, 17 June 2019

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



from Use of attr reference in selector StateListDrawable

No comments:

Post a Comment