简体   繁体   中英

How to change the progress colour of SeekBar dynamically at runtime?

I have tried to find a solution to this problem, but nothing i try seems to give the effect i want.

In my app, i have a SeekBar, that i want to change the colour of to match the current file being played (user can set different colours for different files). My problem is that i am not able to find a way of setting the progress and thumb colour of the seekbar at runtime, as i dont know what colours will be used until the app runs, so i cant create XML drawables for each colour.

If someone knows of a way of changing the colour of the progress and thumb of the SeekBar at runtime, I would much appreciate their help.

Thanks, Corey B

For those who have same problem. From above code i understand you want to change colour of SeekBar i had same problem , after lot of search i made method of my own.

public void changeSeekbarColor(SeekBar s,int colorp,int colors,int color b)
{
    PorterDuff.Mode mMode = PorterDuff.Mode.SRC_ATOP;

    LayerDrawable layerDrawable = (LayerDrawable) s.getProgressDrawable();
    Drawable progress = (Drawable) layerDrawable.findDrawableByLayerId(android.R.id.progress);
    Drawable secondary = (Drawable) layerDrawable.findDrawableByLayerId(android.R.id.secondaryProgress);
    Drawable background = (Drawable) layerDrawable.findDrawableByLayerId(android.R.id.background);
    Drawable th = s.getThumb();

    // Setting colors
    progress.setColorFilter(colorp,mMode);
    secondary.setColorFilter(colors,mMode);
    background.setColorFilter(colorb, mMode);
    th.setColorFilter(colorp,mMode);

    // Applying Tinted Drawables 
    layerDrawable.setDrawableByLayerId(android.R.id.progress, progress);

    layerDrawable.setDrawableByLayerId(android.R.id.secondaryProgress, secondary);

    layerDrawable.setDrawableByLayerId(android.R.id.background, background);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM