Android - Tinting ProgressBar on pre-Lollipop devices

Geryson picture Geryson · Apr 6, 2017 · Viewed 13.2k times · Source

My app's minimum API-level is 19 (KitKat), and contains a layout with a horizontal ProgressBar. I'm using android:progressTint attribute to tint the bar to a custom color. It works pretty well on Lollipop (API 21) and above, but below (for example on API 19) it doesn't; it shows up in a different color. The reason is that this attribute

is only used in API level 21 and higher

as Android Studio states.

So I'm wondering what can be a good alternative for tinting the ProgressBar on pre-Lollipop devices, too. Can it be made inside the layout file with XML? Or should this be done on a different way?


EDIT #1: My ProgressBar is used to show concrete values in percents, not for indicating that my layout is loading. I want to make this clear because after trying what Kuldeep Kulkarni wrote below, my bar looked like a material loading indicator (of course on the Lollipop device, not any visible result on KitKat device).

Answer

Satan Pandeya picture Satan Pandeya · Apr 6, 2017

You can wrap with ProgressBar indeterminateDrawable method for pre-Lollipop.

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {

     Drawable drawableProgress = DrawableCompat.wrap(progressBar.getIndeterminateDrawable());
     DrawableCompat.setTint(drawableProgress, ContextCompat.getColor(getContext(), android.R.color.holo_green_light));
     progressBar.setIndeterminateDrawable(DrawableCompat.unwrap(drawableProgress));

} else {
    progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), android.R.color.holo_green_light), PorterDuff.Mode.SRC_IN);
}