Q: Android change shape drawable color I want to change the gradient color of my progress bar. Right now, I can change it to a solid color but I cannot change it to a gradient color.
LayerDrawable progressDrawable = (LayerDrawable) myProgressBar.getProgressDrawable();

// get the color drawable of the progress bar
Drawable primaryColor = progressDrawable.getDrawable(1); 

// set the new color. 
primaryColor.setColorFilter(ContextCompat.getColor(getContext(), R.color.yellow), PorterDuff.Mode.SRC_OVER);

Instead of a solid color I want to change the gradient color.
EDIT My Drawable file
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@android:id/background">
    <shape>
        <corners android:radius="5dip" />
        <gradient
            android:startColor="@color/gray300"
            android:centerColor="@color/gray400"
            android:centerY="0.75"
            android:endColor="@color/gray500"
            android:angle="270"
            />
    </shape>
</item>

<item
    android:id="@+id/progress">
    <clip>
        <shape>
            <corners
                android:radius="5dip" />
            **I want to change this gradient color**
            <gradient 
            android:startColor="#33FF33"
            android:endColor="#008000"
            android:angle="270" />
        </shape>
    </clip>
</item>

</layer-list>


A: try this
GradientDrawable bgShape = (GradientDrawable) myProgressBar.getBackground().getCurrent();
bgShape.setColor(Color.YELLOW);


A: Here's my implementation: Draws an animated rainbow gradient. Cool, if I do say so myself. It's not XML-based, but could be made to be, and it does use gradients. Perhaps it will give you some ideas.
Setup:
 pb = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
GradientDrawable rainbow = new GradientDrawable(Orientation.LEFT_RIGHT,
        new int[] {Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED});

AnimationDrawable ad = getProgressBarAnimation();
pb.setBackgroundDrawable(ad);

And animate:
private AnimationDrawable getProgressBarAnimation(){

GradientDrawable rainbow1 = new GradientDrawable(Orientation.LEFT_RIGHT,
        new int[] {Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW});

GradientDrawable rainbow2 = new GradientDrawable(Orientation.LEFT_RIGHT,
        new int[] { Color.YELLOW, Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN});          

GradientDrawable rainbow3 = new GradientDrawable(Orientation.LEFT_RIGHT,
        new int[] { Color.GREEN, Color.YELLOW, Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN });

GradientDrawable rainbow4 = new GradientDrawable(Orientation.LEFT_RIGHT,
        new int[] { Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED, Color.MAGENTA, Color.BLUE });

GradientDrawable rainbow5 = new GradientDrawable(Orientation.LEFT_RIGHT,
        new int[] { Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED, Color.MAGENTA });

GradientDrawable rainbow6 = new GradientDrawable(Orientation.LEFT_RIGHT,
        new int[] {Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED });


GradientDrawable[]  gds = new GradientDrawable[] {rainbow1, rainbow2, rainbow3, rainbow4, rainbow5, rainbow6};

AnimationDrawable animation = new AnimationDrawable();

for (GradientDrawable gd : gds){
    animation.addFrame(gd, 100);

}

animation.setOneShot(false);

return animation;

}

