0

I am going to change color of the SeekBar with below code.

public void setColor(SeekBar view, Integer color) {
    LayerDrawable drawable = (LayerDrawable) view.getProgressDrawable().getCurrent();

    Drawable progress = drawable.findDrawableByLayerId(android.R.id.progress);

    progress.setColorFilter(color, PorterDuff.Mode.SRC_IN);
}

Some of devices ran the code with no error. But some of devices will return java.lang.ClassCastException android.graphics.drawable.NinePatchDrawable cannot be cast to android.graphics.drawable.LayerDrawable.

Is anyone have workaround for this issue ?

Ali Khaki
  • 1,184
  • 1
  • 13
  • 24
Dicky Chan
  • 235
  • 2
  • 13

1 Answers1

0

You should use your custom png for this purpose and implement it like this:

drawable/color_progress.xml

<item
    android:id="@android:id/background"
    android:drawable="@drawable/static_slider"/>
<item android:id="@android:id/secondaryProgress">
    <scale
        android:drawable="@drawable/static_slider"
        android:scaleWidth="100%" />
</item>
<item android:id="@android:id/progress">
    <scale
        android:drawable="@drawable/active_slider"
        android:scaleWidth="100%" />
</item>

and use inside your seekbar like this

 <SeekBar
  ......
 android:progressDrawable="@drawable/color_progress" />

static_slider and active_slider are different PNGs for empty and filled state. Hope this helps.

Waqar Khan
  • 468
  • 4
  • 18