I want to change my progressbar progress color based on the value of it. I have a gradient background which is created in XML.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dp"/>
<gradient
android:endColor="#55850000"<!-- green -->
android:centerColor="#55e6ff00" <!-- yellow -->
android:startColor="#5500c872" <!-- red -->
/>
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dp"/>
<gradient
android:endColor="#c20600"<!-- green %100 alpha -->
android:centerColor="#e6ff00"<!-- yellow %100 alpha-->
android:startColor="#00c872"<!-- red %100 alpha -->
/>
</shape>
</clip>
</item>
</layer-list>
My problem is this, I want to change the whole progress color based on the same of the background color.Let's say my progress comes to %60, whole progress bar should be color of yellow. If my progress comes to %90, whole progress bar should be color of red. It should change dynamically and it should be the same color with the background color of progress.
This is the animation color I used which is not working!
public class ProgressBarAnimation extends Animation{
private ProgressBar progressBar;
private float from;
private float to;
public ProgressBarAnimation(ProgressBar progressBar, float from, float to) {
super();
this.progressBar = progressBar;
this.from = from;
this.to = to;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
float value = from + (to - from) * interpolatedTime;
/*if(value < 25){
progressBar.getIndeterminateDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
}else if(value <=50){
progressBar.getIndeterminateDrawable().setColorFilter(Color.YELLOW, PorterDuff.Mode.MULTIPLY);
}else{
progressBar.getIndeterminateDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
}*/
// NEEDS TO CHANGE COLOR IN HERE !
progressBar.setProgress((int) value);
}
}