Search code examples
javaandroidseekbar

how to change SeekBar color in android? (Programmatically)


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

Solution

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