Skip to content Skip to sidebar Skip to footer

Java.lang.classcastexception: Android.graphics.drawable.layerdrawable Cannot Be Cast To Android.graphics.drawable.gradientdrawable

In my application I am trying change background color for each listView item. And for that I am using shapes which in layer-list. Here is my code drop_shadow.xml

Solution 1:

you can create gradient drawable dynamically.. use below class

import android.graphics.drawable.GradientDrawable;

    publicclassSomeDrawableextendsGradientDrawable {

    publicSomeDrawable(int pStartColor, int pCenterColor, int pEndColor, int pStrokeWidth, int pStrokeColor, float cornerRadius) {
        super(Orientation.BOTTOM_TOP,newint[]{pStartColor,pCenterColor,pEndColor});
        setStroke(pStrokeWidth,pStrokeColor);
        setShape(GradientDrawable.RECTANGLE);
        setCornerRadius(cornerRadius);
    }
 }

and use this class as below

SomeDrawabledrawable=newSomeDrawable(Color.parseColor("Start Color Code"),Color.parseColor("Center Color Code"),Color.parseColor("End Color Code"),1,Color.BLACK,00);
yourLayout.setBackgroundDrawable(drawable);

Solution 2:

They are both a subclass of Drawable, so you can't cast them to each other, only to their parent classes.

Solution 3:

i guess it's better to use separacte colors.xml file for the color of the shape, and use tag, you can delete the .

after all your shape both is the same which is rectangle.

previously i got the same error because i'm using and inside the same xml.

Post a Comment for "Java.lang.classcastexception: Android.graphics.drawable.layerdrawable Cannot Be Cast To Android.graphics.drawable.gradientdrawable"