Change progressbar color through CODE ONLY in Android
This will help much no need to do so much coding :)
ProgressBar spinner = new android.widget.ProgressBar( context, null, android.R.attr.progressBarStyle);spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000,android.graphics.PorterDuff.Mode.MULTIPLY);
In the case that you need to tint the background and the progress bar in different colors.
progress_drawable.xml
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape android:shape="rectangle" > <solid android:color="@color/white" /> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape> <solid android:color="@color/green" /> </shape> </clip> </item></layer-list>
Its possible, programmatically, to decompound its layer-list items, and tint them separately:
LayerDrawable progressBarDrawable = (LayerDrawable) progressBar.getProgressDrawable();Drawable backgroundDrawable = progressBarDrawable.getDrawable(0);Drawable progressDrawable = progressBarDrawable.getDrawable(1);backgroundDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.white), PorterDuff.Mode.SRC_IN);progressDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.red), PorterDuff.Mode.SRC_IN);
Update
In newer versions of Android (21 works), you can change the color of a progressbar programmatically by just using setProgressTintList
.
To set it red, use:
//bar is a ProgressBarbar.setProgressTintList(ColorStateList.valueOf(Color.RED));