Skip to content Skip to sidebar Skip to footer

Color State List Not Recognized In Shape Drawable

I define following drawable my_background_drawable.xml:

Solution 1:

A ColorStateList cannot be passed as an attribute for <solid> in an XML definition, or really any attribute of a <shape>. This attribute is inflated out of the XML as a Color resource and then passed to the Drawable's setColor() method, which only takes a single ARGB value.

There is only one type of Drawable instance that is designed to contain and present multiple items based on state, and that is StateListDrawable, which is what you get when you inflate a <selector>. All other Drawable instances are meant to simply be members of this collection or drawn standalone.

Note also that an inflated <shape> item is actually a GradientDrawable and not a ShapeDrawable. If you check out the inflate() method of GradientDrawablein the source, you can get all the detail you could ask for on how each attribute is used.

HTH!

Solution 2:

In fact you can assign a ColorStateList as a solid color inside of the xml of a a shape -> GradientDrawable, but that is only a new feature in Lollipop.

Older versions of GradientDrawable only accept color resources.

Currently working on a compat alternative if you are interested.

Solution 3:

You are doing it wring .... just Replace this

   android:color="@color/color_stateful"

with

android:background="@color/color_stateful"

update:

in your program code in the my_background_drawable.xml

<?xml version="1.0" encoding="utf-8"?><layer-listxmlns:android="http://schemas.android.com/apk/res/android" ><item><shapeandroid:gravity="center"android:shape="rectangle"><solidandroid:background="@color/color_stateful" /><!--this is the chanage i made... here--></shape></item><itemandroid:drawable="@drawable/selector_png_drawable" /></layer-list>

Post a Comment for "Color State List Not Recognized In Shape Drawable"