0

I have a View with a ShapeDrawable as its background, and I want to be able to change only the background color (the "solid" part) of the Drawable, without changing the other part (such as "stroke"). Is there a way to accomplish that? Thanks.

user940016
  • 2,878
  • 7
  • 35
  • 56

2 Answers2

0

How about setColorFilter().

You can use that in combination with invalidate() so that the drawable gets redrawn. Take a look at these links for reference

ShapeDrawable colorfilter

Drawable InvalidateSelf

I also came across a nice post on stack. Take a look at this as well. How to change colors of Android drawable

Community
  • 1
  • 1
Orlymee
  • 2,349
  • 1
  • 22
  • 24
0

I found a solution: Actually in my case I have defined the drawable as an XML file, and Android converted it to a GradientDrawable object, not a ShapeDrawable, so I cast it to its RTT (run-time type), and then used its setColor function:

GradientDrawable colorBackground=(GradientDrawable)currentColorView.getBackground();
colorBackground.setColor(color);

As I said, it works only for GradientDrawable, so for other drawable types, another way must be searched.

user940016
  • 2,878
  • 7
  • 35
  • 56