In my app I define color attributes that I set in a custom theme:
res/values/attrs.xml
<resources>
<attr name="bbColorPrimary" format="color|reference" />
</resources>
res/values/colors.xml
<resources>
<color name="white">#ffffff</color>
</resources>
res/values/style.xml
<style name="MyStyle" parent="@style/Theme.AppCompat.NoActionBar">
<item name="bbColorPrimary">@color/white</item>
</style>
res/drawable/background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
<shape>
<solid android:color="?attr/bbColorPrimary" />
</shape>
</item>
<item>
<shape>
<solid android:color="@color/transparent" />
</shape>
</item>
</selector>
res/drawable/background.xml
is set as the background of some button. The app crashes while inflating the res/drawable/background.xml
file, with the following exception:
...
Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
at android.content.res.TypedArray.getColor(TypedArray.java:326)
at android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java:748)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:172)
....
It works on API levels higher than 10. If I remove the ?attr/bbColorPrimary
it works fine, although many other ?attr/..
calls are set in other resource files. I am not using any version-specific resource folders.
What am I doing wrong here?
I found an answer on SO, mentioning that ?attr/..
are not supported in xml drawables. See here: https://stackoverflow.com/a/13471695/169748
Apparently that is true at least for API <= 10.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With