Lollipop Progress Bar Tinting

25,768

Solution 1

The accepted solution wasn't working for me on pre-Lollipop, but I found this solution to fit all APIs and on top of that, it doesn't use any deprecated code:

Java solution

// 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);
}

Kotlin extension

fun ProgressBar.setIndeterminateTintCompat(@ColorInt color: Int) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        val wrapDrawable = DrawableCompat.wrap(indeterminateDrawable)
        DrawableCompat.setTint(wrapDrawable, color)
        indeterminateDrawable = DrawableCompat.unwrap(wrapDrawable)
    } else {
        indeterminateTintList = ColorStateList.valueOf(color)
    }
}

// usage

val desiredColor = ContextCompat.getColor(context, R.color.myColor)
myProgressBar.setIndeterminateTintCompat(desiredColor)

Happy coding !

Solution 2

Try changing your ProgressBar to this

<ProgressBar
android:id="@+id/view_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="50dp"
android:layout_height="50dp"
android:indeterminate="true"
android:indeterminateTintMode="src_in"
android:indeterminateTint="@color/red"
/>

Also you can do it programmaticaly doing this :

ProgressBar progressBar;
progressBar = (ProgressBar)findViewById(R.id.view_progress);
progressBar.getProgressDrawable().setColorFilter(Color.RED, Mode.SRC_IN);

References

Setting in ProgressBar Tint color

Solution 3

To avoid tinting the ProgressBar background as well, you should get a reference to the exact bitmap from the inner ProgressBar LayerDrawable. This is a hacky way of doing it, but it works.

LayerDrawable layerDrawable = (LayerDrawable) progressBar.getProgressDrawable();
Drawable drawable = layerDrawable.getDrawable(2); // The progress bitmap
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);

Solution 4

Your code works well in Android 6.0+, but not for Android 5.1 and before as I have tested on my local Android emulator.

I agree with the explanation of @Skizo-ozᴉʞS, and I've found an issue report in Android Issue Tracker that may refer to this bug. See this link.

From the code it seems that using "android:indeterminateTint" only will not take any effects for API < 23.

Share:
25,768
Jumpa
Author by

Jumpa

Updated on November 04, 2020

Comments

  • Jumpa
    Jumpa over 3 years

    I'm on a Lollipop device (MotoG 2014), I read about progress bar tinting, but this is not working...I get the default progress bar color. What am I missing here?

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:backgroundTintMode="src_in"
        android:indeterminate="true"
        android:indeterminateTint="#f00" />
    

    Many thanks.

  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS almost 9 years
    I'm glad to help you :)
  • Boris
    Boris almost 7 years
    @Ninja Haven't tried this yet, but I guess it would have to be progressBar.getIndeterminateDrawable() to be in line with the XML sample.
  • channae
    channae almost 6 years
    This is the easiest solution I found for pre-lollipop devices. Thanks
  • BoD
    BoD over 5 years
    It looks like src_in is the default, so no need to set it
  • Punyapat
    Punyapat over 5 years
    This is just what I'm looking for.
  • user3701188
    user3701188 about 5 years
    Why is everyone missing the question the question is why this is not working on Lollipop as described by the documentation and everyone
  • Henrique de Sousa
    Henrique de Sousa about 5 years
    well dear user3701188, once again you'll have to ask Google about that :)
  • Quentin G.
    Quentin G. almost 5 years
    I think your condition should be if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { ? I have tested this and it works for me only if the first piece of code is executed for api over 21.
  • guillaume-tgl
    guillaume-tgl over 3 years
    Based on this issue report, you could tell that one option for the indeterminateTint to work is to also add android:indeterminateTintMode="src_in".