how to change SeekBar color in android? (Programma

2020-07-08 02:49发布

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);

8条回答
等我变得足够好
2楼-- · 2020-07-08 02:59

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);
查看更多
▲ chillily
3楼-- · 2020-07-08 03:00

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);

查看更多
Evening l夕情丶
4楼-- · 2020-07-08 03:03

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);
查看更多
女痞
5楼-- · 2020-07-08 03:05
    //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));
查看更多
该账号已被封号
6楼-- · 2020-07-08 03:06

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.

查看更多
【Aperson】
7楼-- · 2020-07-08 03:12

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);
}
查看更多
登录 后发表回答