How do I dynamically change the color of a drawable <shape>? (Android)

2.5k views Asked by At

Each button represents something clickable, which has a color stored in the database that a user can configure. The background of the button is the <shape> that follows.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
  <stroke android:width="1dp" android:color="#000" />
  <corners android:radius="5dp" />
  <gradient android:startColor="#FFF"
            android:centerColor="#FFF"
            android:endColor="#666"
            android:angle="315" />
  <padding android:bottom="2dp"
           android:left="2dp"
           android:right="2dp"
           android:top="2dp" />
</shape>

I'd like to get at the <gradient> element — specifically the startColor centerColor and endColor attributes. I can do this:

button.getBackground().setTint(theColor);

But that seems to wipe out the stroke and gradient.

Is this possible? Is there a better way?

3

There are 3 answers

2
Bhavnik On BEST ANSWER

try below snippet, give it a try

Have a look at this screenshot

GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{ContextCompat.getColor(this,R.color.white), ContextCompat.getColor(this,R.color.red_500), ContextCompat.getColor(this,R.color.blue_500)});
    gd.setShape(GradientDrawable.RECTANGLE);
    gd.setStroke(1, ContextCompat.getColor(this, R.color.black));
    gd.setCornerRadius(5f);
    gd.setBounds(2, 2, 2, 2);
    findViewById(R.id.btn_analytics).setBackground(gd);
0
Vishnu V S On

Please check this

GradientDrawable bgShape = (GradientDrawable)button.getBackground();
bgShape.setColor(Color.BLACK);
0
Travis Well On

As previous answers have pointed out, it is a GradientDrawable which, it turns out, has a fancy setColors method, allowing direct access to those three xml attributes in order. But it must be preceded by a call to mutate like so:

GradientDrawable gd = ((GradientDrawable) button.getBackground());
gd.mutate();
gd.setColors(new int[]{dynamicColor,
   ContextCompat.getColor(context, R.color.button),
   ContextCompat.getColor(context, R.color.button_shade)});

@Bhavnik got me halfway there, so thanks.