I have a progressBar using the ProgressBar class.
Just doing this:
progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizont
Layout = activity_main.xml:
<ProgressBar
android:id="@+id/circle_progress_bar_middle"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:max="100"
android:rotation="-90"
android:indeterminate="false"
android:progressDrawable="@drawable/my_drawable_settings2" />
In Java Activity/Fragment:
ProgressBar myProgressBar = (ProgressBar) view.findViewById(R.id.circle_progress_bar_middle);
myProgressBar.setProgressDrawable(getResources().getDrawable(R.my_drawable_settings1));
The my_drawable_settings1.xml file inside your drawable/mipmap folder:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/progress">
<shape
android:innerRadius="55dp"
android:shape="ring"
android:thickness="9dp"
android:useLevel="true">
<gradient
android:startColor="#3594d1"
android:endColor="@color/white"
android:type="sweep" />
</shape>
</item>
</layer-list>
Where my_drawable_settings1 and my_drawable_settings2.xml has different colors.
This works for me with AppCompat:
DrawableCompat.setTint(progressBar.getProgressDrawable(), tintColor);
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 ProgressBar
bar.setProgressTintList(ColorStateList.valueOf(Color.RED));
progressbar.setIndeterminateTintList(ColorStateList.valueOf(Color.RED));
It only works above API 21
setColorFilter
with 2 arguments is deprecated and the other answer pointing to use LightingColorFilter
neither worked for me so
val progressBar = ProgressBar(context, null, android.R.attr.progressBarStyle).apply {
val colorFilter = PorterDuffColorFilter(
ContextCompat.getColor(context, R.color.yourColor),
PorterDuff.Mode.MULTIPLY
)
indeterminateDrawable.colorFilter = colorFilter
}
That will programmatically give you the circular progress bar with your color
This post is what you're looking for: How to change progress bar's progress color in Android
If you want the user to choose their own colors, just make multiple-drawable XML files for each color, and select them based on the user's choice.