Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the color of a seekbar on onProgressChanged

I have custom seekbar and I can change the the color in the onProgressChanged by setting a new progress drawable:

seek.setProgressDrawable(res.getDrawable(R.drawable.seekbar_bg1));

But I would like a seekbar which has a solid color from green to red depending on the progress. Is there a way to use something like a gradient color so I don't need to create like 100 drawables this?

    <?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>
    <solid android:color="@android:color/transparent" />
  </shape>
</item>

<item android:id="@+id/progressshape">
<clip>
  <shape>
    <solid android:color="@color/custom_yellow" />
  </shape>
</clip>
</item>

</layer-list>

For those who are interested in the solution. I use the following code:

this method to set the color:

public void setProgressBarColor(int newColor){ 
  LayerDrawable ld = (LayerDrawable) getProgressDrawable();
  ClipDrawable d1 = (ClipDrawable) ld.findDrawableByLayerId(R.id.progressshape);
  d1.setColorFilter(newColor, PorterDuff.Mode.SRC_IN);
}

and in the onProgressChanged:

 if(progress <= 50){
                seek.setProgressBarColor(Color.rgb( 255 - (255/100 * (100 - progress*2)), 255, 0));
 }else{
                seek.setProgressBarColor(Color.rgb( 255, 255 - (255/100 * (progress - 50)*2), 0));
 }
like image 730
Luciano Avatar asked Mar 23 '12 10:03

Luciano


People also ask

How do I change the color of my SeekBar track?

If you are using default SeekBar provided by android Sdk then their is a simple way to change the color of that . just go to color. xml inside /res/values/colors. xml and change the colorAccent.

How do I change my thumb SeekBar?

Android supports it via xml. Just add android:thumbTint="@color/yourColor" in your seekbar.

How do I set the value of SeekBar?

One possible solution would be to set seekBar. setMax(20) (or android:max="20" in XML), and whenever you use or display the value, multiply it by 10. The SeekBar would then appear to move at least 20 at a time.

How can I make SeekBar thicker?

You can use the Slider provided by the Material Components Library. Use the app:trackHeight="xxdp" (the default value is 4dp ) to change the height of the track bar. It requires the version 1.2. 0 of the library.


2 Answers

I do something like this using this method:

public void setProgressBarColor(ProgressBar progressBar, int newColor){ 
    LayerDrawable ld = (LayerDrawable) progressBar.getProgressDrawable();
    ClipDrawable d1 = (ClipDrawable) ld.findDrawableByLayerId(R.id.progressshape);
    d1.setColorFilter(newColor, PorterDuff.Mode.SRC_IN);

}

Then, when you update the progress, you just have to change the desired color.

like image 122
Alesqui Avatar answered Nov 15 '22 08:11

Alesqui


Use this :

bar.setProgressDrawable(new ColorDrawable(Color.rgb( red, green, blue)));

Change the red,green,blue with progress change.

like image 34
asish Avatar answered Nov 15 '22 08:11

asish