How to change shape color dynamically? How to change shape color dynamically? android android

How to change shape color dynamically?


You could modify it simply like this

GradientDrawable bgShape = (GradientDrawable)btn.getBackground();bgShape.setColor(Color.BLACK);


For me, it crashed because getBackground returned a GradientDrawable instead of a ShapeDrawable.

So i modified it like this:

((GradientDrawable)someView.getBackground()).setColor(someColor);


This works for me, with an initial xml resource:

example.setBackgroundResource(R.drawable.myshape);GradientDrawable gd = (GradientDrawable) example.getBackground().getCurrent();gd.setColor(Color.parseColor("#000000"));gd.setCornerRadii(new float[]{30, 30, 30, 30, 0, 0, 30, 30});gd.setStroke(2, Color.parseColor("#00FFFF"), 5, 6);

Result of the above: http://i.stack.imgur.com/hKUR7.png