SoFunction
Updated on 2025-03-10

Android dynamically changes the SeekBar progress bar color and slider color instance code

When encountering a dynamic change of the SeekBar progress bar color and slider color, some of which are changed to different colors according to different progress.

How to do this? Everyone knows that if you set progressDrawable and thumb, it is certain to set it up like this. Dynamic changes need to be implemented in the code.

Use shape progress bar and slider

SeekBar Settings

SetProgressDrawable and setThumb dynamically in the code

Everyone is familiar with drawing graphics. Background is the background image, secondaryProgress second progress bar, progress progress bar:

<layer-list xmlns:andro> 
<item android:>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="@color/gray_cc"
android:centerColor="@color/gray_cc"
android:centerY="0.75"
android:endColor="@color/gray_cc"
android:angle="270"/>
</shape>
</item>
< !-- Mine has no second background,Therefore, the second background image is not drawn -->
<item android:>
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"/>
</shape>
</clip>
</item>
<item android:>
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="@color/gray_cc"
android:centerColor="@color/gray_cc"
android:centerY="0.75"
android:endColor="@color/gray_cc"
android:angle="270"/>
</shape>
</clip>
</item>
</layer-list>

Then draw the slider:

<selector xmlns:andro>
<item android:drawable="@drawable/seekbar_thumb_gray" android:state_focused="true" android:state_pressed="true" />
<item android:drawable="@drawable/seekbar_thumb_gray" android:state_focused="false" android:state_pressed="false" />
<item android:drawable="@drawable/seekbar_thumb_gray" android:state_focused="true" android:state_pressed="false" />
<item android:drawable="@drawable/seekbar_thumb_gray" android:state_focused="true" />
</selector>

Finally, set in xml:

android:progressDrawable="@drawable/seekbar_light"
android:thumb="@drawable/seekbar_thumb"

Here, the slider slides to 0 or the maximum may not be displayed, and the slider will only be displayed halfway; some of the background is too high; so these need to be set:

android:maxHeight="5dp"
android:minHeight="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"

The above maxHeight and minHeight can make the background less high, and changing the value can control the height.

paddingLeft and paddingRight are preferably half the width of the slider, so that the display can be fully displayed.

Let’s get to the point where you set the color dynamically in the code.

We can use getProgressDrawable in the code to get the progress background, so we can change it using this.

Since we get LayerDrawable, it contains multiple levels, of course we only draw three levels of background above; if we are not sure, we can loop through the ID to make judgments:

//Get seerbar hierarchical drawable objectLayerDrawable layerDrawable = (LayerDrawable) ();
// How many levels are there (up to three)int layers = ();
Drawable[] drawables = new Drawable[layers];
for (int i = 0; i &lt; layers; i++) {
switch ((i)) {
// If it is seekbar backgroundcase :
drawables[i] = (0);
break;
// If it is the first progress of the drag barcase :
//This is the dynamic color valuedrawables[i] = new PaintDrawable(progressColor);
drawables[i].setBounds((0).getBounds());
break;
...
}
}
(new LayerDrawable(drawables));
(thumb);
();

You can get different backgrounds above, and then dynamically change the settings. The above code can complete the top picture. Because the background does not need to change the color, the background picture will not change. After getting the progress bar background, we use PaintDrawable to draw the color. Of course, you can use other construction of a background Drawable and then set the style you need. The slider can also be a background image, or you can use getThumb to reset the background if you give the progress poor background.

I mentioned in the previous article that Android changed the color of solid color background image. If you are not clear, you can click me in to check Android changed the color of solid color background image.

Because we are also pure color here, this is easier to do and simpler:

//Get seerbar hierarchical drawable objectLayerDrawable layerDrawable = (LayerDrawable) ();
//Because the second progress background image is not drawn when drawing the background image, it is directly 1Drawable drawable = (1);
(progressColor, );
//Get the slider backgroundDrawable thumb = ();
(thumbColor, );
();

From the above, we can use the method of changing the solid color to change the background color, of course, the premise is that your background picture is also a solid color.

This will make you enjoyable again.

Notice

The first solution can draw a variety of styles, mainly depending on your use of Drawable. However, if you use the background image, if there are arcs on the left and right ends of the progress, you need to set an arc when drawing dynamically. It is estimated that the implementation is not simple, otherwise the background image will not have an arc.

The second solution can be used for solid colors, but it is difficult to work for complex and diverse styles.

The above is the example code for dynamically changing the color of SeekBar progress bar and slider color of Android introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!