How to change progress bar's progress color in Android

前端 未结 30 1954
情话喂你
情话喂你 2020-11-22 07:49

I\'m using an horizontal progress bar in my Android application, and I want to change its progress color (which is Yellow by default). How can I do it using code

30条回答
  •  无人及你
    2020-11-22 08:33

    Nowadays in 2016 I found some pre-Lollipop devices don't honour the colorAccent setting, so my final solution for all APIs is now the following:

    // fixes pre-Lollipop progressBar indeterminateDrawable tinting
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    
        Drawable wrapDrawable = DrawableCompat.wrap(mProgressBar.getIndeterminateDrawable());
        DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(getContext(), android.R.color.holo_green_light));
        mProgressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
    } else {
        mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), android.R.color.holo_green_light), PorterDuff.Mode.SRC_IN);
    }
    

    For bonus points, it doesn't use any deprecated code. Try it!

提交回复
热议问题