Search code examples
androidseekbarxml-drawableandroid-selector

drawable xml shape color change dyanamically


I am using the SeekBar, I need to use the different color SeekBar's progress in the list.

So I need to change the drawable shape color programmatically

this is the drawale xml. seekbar_points.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@android:id/background"
    android:drawable="@drawable/points_bg" />
<item android:id="@android:id/progress">
    <clip android:drawable="@drawable/points_progress" />
</item>
</layer-list>

points_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:width="6dp" android:color="@color/colorAccent" />
</shape>

points_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:width="6dp" android:color="@color/color7" />
</shape>

enter image description here

I have to change this green color area dynamically. for this I have set the color points_progress.xml in this xml

Please suggest the idea Thanks in advance....


Solution

  • Use this:

    seekBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(сontext, R.color.color_new), PorterDuff.Mode.SRC_IN);
    

    Update:
    If you want to change color for progressed area only:

    LayerDrawable progressDrawable = (LayerDrawable) seekBar.getProgressDrawable();
    Drawable filledDrawable = progressDrawable.findDrawableByLayerId(android.R.id.progress);
    filledDrawable.setColorFilter(ContextCompat.getColor(сontext, R.color.color_new), PorterDuff.Mode.SRC_IN);