Lollipop Progress Bar Tinting
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 !
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
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);