Change progressbar color through CODE ONLY in Android

I have a progressBar using the ProgressBar class.

Just doing this:

progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);

I need to change the color of that one, using input value like so:

int color = "red in RGB value".progressBar.setColor(color)

or something like that...

I can't use an XML layout because the progress bar is customizable for users.

77318 次浏览

It is possible to colorize the Progress bar by setting the color filter on the progress bar drawable:

Drawable drawable = progressBar.getProgressDrawable();
drawable.setColorFilter(new LightingColorFilter(0xFF000000, customColorInt));

As I found help on a topic here but can't remember the link, I'm posting my full solution which works great for my needs:

    // Draw a simple progressBar from xml
progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);


// Convert the color (Decimal value) to HEX value: (e.g: #4b96a0)
String color = colorDecToHex(75, 150, 160);


// Define a shape with rounded corners
final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
ShapeDrawable pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners,     null, null));


// Sets the progressBar color
pgDrawable.getPaint().setColor(Color.parseColor(color));


// Adds the drawable to your progressBar
ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
progressBar.setProgressDrawable(progress);


// Sets a background to have the 3D effect
progressBar.setBackgroundDrawable(Utils.getActivity().getResources()
.getDrawable(android.R.drawable.progress_horizontal));


// Adds your progressBar to your layout
contentLayout.addView(progressBar);

And here is the code to convert DECIMAL color values to HEXADECIMAL:

public static String colorDecToHex(int p_red, int p_green, int p_blue)
{
String red = Integer.toHexString(p_red);
String green = Integer.toHexString(p_green);
String blue = Integer.toHexString(p_blue);


if (red.length() == 1)
{
red = "0" + red;
}
if (green.length() == 1)
{
green = "0" + green;
}
if (blue.length() == 1)
{
blue = "0" + blue;
}


String colorHex = "#" + red + green + blue;
return colorHex;
}

I think the last method is not that clean but it works well.

Hope this well help, too much time wasted on this progressbar.

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

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

This works for me with AppCompat:

DrawableCompat.setTint(progressBar.getProgressDrawable(), tintColor);

I have given default color in xml by drawable.

I have changed it programatically.

activity_splasg.xml:

<ProgressBar
android:id="@+id/splashProgressBar"
android:progressDrawable="@drawable/splash_progress_drawable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
style="?android:attr/progressBarStyleHorizontal"
android:layout_alignParentBottom="true" />

splash_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>
<solid
android:color="@android:color/transparent" />
</shape>
</item>


<item
android:id="@android:id/progress">
<clip>
<shape>
<solid
android:color="#e5c771" />
</shape>
</clip>
</item>


</layer-list>

Now How to change ProgressDrawable color programatically.

ProgressBar splashProgressBar = (ProgressBar)findViewById(R.id.splashProgressBar);


Drawable bgDrawable = splashProgressBar.getProgressDrawable();
bgDrawable.setColorFilter(Color.BLUE, android.graphics.PorterDuff.Mode.MULTIPLY);
splashProgressBar.setProgressDrawable(bgDrawable);

Hope this will help you.

progressbar.setIndeterminateTintList(ColorStateList.valueOf(Color.RED));

It only works above API 21

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

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.

if you want to change color of progress bar programmatically then you copy past this code it is working 100%

 mainProgressBar.getIndeterminateDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);

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

Try this:

progress_wheel.getIndeterminateDrawable().setColorFilter(Color.parseColor(getPreferences().getString(Constant.SECOND_COLOR, Constant.SECONDARY_COLOR)), android.graphics.PorterDuff.Mode.MULTIPLY);

Kotlin 2021:

progressBar.indeterminateTintList = ColorStateList.valueOf(Color.WHITE)

You can also pass a resource with getColor()

You might need these 2 in your case instead if it's not indeterminate:

setSecondaryProgressTintList


setProgressTintList

Try this Code:

   CircularProgressIndicator  circularProgressIndicator = findViewById(R.id.tenant_progress_color);
circularProgressIndicator.setIndicatorColor(color);
circularProgressIndicator.setTrackColor(R.color.gray);