Change progressbar color through CODE ONLY in Android

AndroidColorsProgress Bar

Android Problem Overview


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.

Android Solutions


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

Solution 2 - Android

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

Solution 3 - Android

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

Solution 4 - Android

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.

Solution 5 - Android

This works for me with AppCompat:

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

Solution 6 - Android

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

Solution 7 - Android

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

Solution 8 - Android

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

Solution 9 - Android

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

It only works above API 21

Solution 10 - Android

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.

Solution 11 - Android

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.

Solution 12 - Android

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

Solution 13 - Android

Try this:

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

Solution 14 - Android

> Try this Code:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhicoView Question on Stackoverflow
Solution 1 - AndroidMit BhattView Answer on Stackoverflow
Solution 2 - AndroidbrbsBrunoView Answer on Stackoverflow
Solution 3 - AndroidJose M VidalView Answer on Stackoverflow
Solution 4 - AndroidhicoView Answer on Stackoverflow
Solution 5 - AndroidLubos IlcikView Answer on Stackoverflow
Solution 6 - AndroidMoritzView Answer on Stackoverflow
Solution 7 - AndroidPir Fahim ShahView Answer on Stackoverflow
Solution 8 - AndroidMerthan ErdemView Answer on Stackoverflow
Solution 9 - AndroidDaniel ParkView Answer on Stackoverflow
Solution 10 - AndroidHiren PatelView Answer on Stackoverflow
Solution 11 - AndroidGeneView Answer on Stackoverflow
Solution 12 - AndroidcutikoView Answer on Stackoverflow
Solution 13 - AndroidKushal PrajapatiView Answer on Stackoverflow
Solution 14 - AndroidManideepView Answer on Stackoverflow