how to change SeekBar color in android? (Programmatically)
Asked Answered
N

9

12

I made an equalizer to go with my app but I am not sure how I can change the seekbar's thumb and progress color. It seems to be pink by default and that doesn't fit my app's aesthetics.

 SeekBar seekBar = new SeekBar(this);

        seekBar.setId(i);

        seekBar.setLayoutParams(layoutParams);
        seekBar.setMax(upperEqualizerBandLevel - lowerEqualizerBandLevel);

        seekBar.setProgress(mEqualizer.getBandLevel(equalizerBandIndex));
        //seekBar.setBackgroundColor(Color.DKGRAY);
        //seekBar.setDrawingCacheBackgroundColor(Color.DKGRAY);
Naker answered 26/7, 2017 at 14:0 Comment(1)
To change in layout see https://mcmap.net/q/108716/-android-styling-seek-bar.Chianti
R
21

To change the color of the Seekbar thumb, create a new style in style.xml

<style name="SeekBarColor"
  parent="Widget.AppCompat.SeekBar"> 
  <item name="colorAccent">@color/your_color</item> 
</style>

Finally in the layout:

<SeekBar
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:theme="@style/SeekBarColor" />

To change the Seekbar progress color, use this in Java Class.

seekBar.getProgressDrawable().setColorFilter("yourcolor", PorterDuff.Mode.MULTIPLY);

Both these will work for API>16.

Edit

To change SeekBar thumb color by Java code.

 seekBar.getProgressDrawable().setColorFilter(getResources().getCo‌​lor(R.color.your_color‌​), PorterDuff.Mode.SRC_ATOP);
Recor answered 26/7, 2017 at 14:10 Comment(4)
Thank you. I used the latter since i made the seekbars for the equalizer programmatically. The progress color changes but i am unable to change the thumb. Anything i can do about that?Naker
The first part of the answers states how to change color of seekbar thumb. I have added this code since it runs over a wider API range.Recor
Check the edited answer for Java code to change seekbar thumb color(works only for android 5 and above).Recor
deprecated in high api levelsBezonian
K
7

While using a style is a good idea, it does not provide much flexibility specially if you want to assign the colors programmatically I suggest:

//for the progress seekBar.getProgressDrawable().setColorFilter(mThemeColor,PorterDuff.Mode.SRC_ATOP); //for the thumb handle seekBar.getThumb().setColorFilter(mThemeColor, PorterDuff.Mode.SRC_ATOP);

Kowalczyk answered 8/4, 2018 at 18:33 Comment(0)
S
2

You can change seekbar thumb and progress colors for programmatically like this:

   seekBar.getProgressDrawable().setColorFilter(Utils.getAccentColor(this), PorterDuff.Mode.SRC_IN);
   seekBar.getThumb().setColorFilter(Utils.getAccentColor(this), PorterDuff.Mode.SRC_IN);
Swayne answered 4/8, 2018 at 7:22 Comment(0)
G
1

You can easily change it via code,

For example:

seekbar.setProgressTintList(ColorStateList.valueOf(Color.parseColor(#000000)));

or

seekbar.setProgressTintList(ColorStateList.valueOf(Color.RED));

Hoping that it will be helpfull for someone in future.

Generalize answered 29/12, 2018 at 10:0 Comment(1)
It has been helpful.Tiros
E
0

You need to call you seekbar like this

SeekBar newSeek = new SeekBar(this, null, R.style.YOUR_NEW_STYLE);

where YOUR_NEW_STYLE will define the colors of the Seekbar that you want.

Embank answered 26/7, 2017 at 14:4 Comment(0)
M
0

try this :

seekBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.mcolor), PorterDuff.Mode.MULTIPLY);
Manysided answered 26/7, 2017 at 14:6 Comment(1)
this does not change the thumbCourlan
P
0

Just a little more peachy answer

public static void setSeekBarColor(SeekBar seekBar, int color) {
    seekBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
    seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
Provision answered 14/11, 2019 at 12:2 Comment(0)
O
0

Kotlin version, and without using deprecated methods:

val colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
    Resources.getSystem().getColor(R.color.your_color, null),
    BlendModeCompat.SRC_ATOP
)

// SeekBar progress color
seekBar.progressDrawable.colorFilter = colorFilter
// SeekBar thumb handle color
seekBar.thumb.colorFilter = colorFilter
Ofelia answered 30/1, 2023 at 18:30 Comment(0)
P
-1
    //seekBar.setBackgroundColor(Color.DKGRAY);
    //seekBar.setDrawingCacheBackgroundColor(Color.DKGRAY);

There are few Views in Android SDK (ProgressBar is another example) in which you have to change colour using graphical color filters, instead of changing Background / Foreground source colour.

Find .setColorFilter() method, supply your source colour into it with some PorterDuff filter, like .Mode.MULTIPLY (depending on what filter mode you like best) and there you go.

Example:

seekBar.setColorFilter(new PorterDuffColorFilter(srcColor,PorterDuff.Mode.MULTIPLY));
Public answered 26/7, 2017 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.