1

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?

Travis Well
  • 947
  • 10
  • 32

3 Answers3

3

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);
Bhavnik
  • 2,020
  • 14
  • 21
  • So basically just reconstructing the XML. In this case I should do away with the XML entirely rather than duplicate the logic. But this approach works, so I'll probably go with it. A necessary change though is to multiply the pixel values by the density. http://stackoverflow.com/questions/4605527/converting-pixels-to-dp Thank you! – Travis Well Dec 15 '16 at 06:29
  • Yeah exactly, you don't need to use xml anymore – Bhavnik Dec 15 '16 at 06:33
1

Please check this

GradientDrawable bgShape = (GradientDrawable)button.getBackground();
bgShape.setColor(Color.BLACK);
Vishnu V S
  • 355
  • 1
  • 10
1

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.

Travis Well
  • 947
  • 10
  • 32