79

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
hico
  • 1,850
  • 1
  • 19
  • 28

14 Answers14

64

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);
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
Mit Bhatt
  • 1,605
  • 2
  • 13
  • 24
53

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));
Jose M Vidal
  • 8,816
  • 6
  • 43
  • 48
  • 11
    You might need to call `setSecondaryProgressTintList` and/or `setIndeterminateTintList` depending on how you use the ProgressBar. – Cristan May 09 '17 at 14:51
  • Works perfectly, and much better than the other answers (only one line of code)! – Jorn Rigter Jul 15 '21 at 14:00
48

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);
brbsBruno
  • 581
  • 6
  • 5
  • 4
    I used this solution but with `DrawableCompat.setTint()` instead of `setColorFilter()` and it worked great. This is the only way I have found so that the background color is not changed also. – Franco Oct 24 '17 at 23:50
  • 1
    @Franco setTint is API 21+ just to be aware. .setColorFilter seems to work for 16+ in my testing. – mikemike396 Apr 17 '18 at 18:38
  • 1
    The only method that worked and did not tint the background!! – Matan Dahan Apr 30 '18 at 10:22
  • This is exactly what I've been looking for, for the past few hours! Thank you so much! Didn't know LayerDrawable existed! – MedievalCoder Dec 15 '20 at 16:58
20

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.

hico
  • 1,850
  • 1
  • 19
  • 28
  • almost works, but if you change the height of the progress bar, the background drawable will be too tall – kenyee May 02 '16 at 23:10
16

This works for me with AppCompat:

DrawableCompat.setTint(progressBar.getProgressDrawable(), tintColor);
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Lubos Ilcik
  • 2,176
  • 1
  • 16
  • 12
15

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));
Moritz
  • 10,124
  • 7
  • 51
  • 61
  • 1
    Thats what I tried. However, using this method, colors are a bit weird... In fact, if I want to just have the color: RGB = #5ea618 with a dark grey background color, what should I do ? Because if for example, I'm doing: new LightingColorFilter(0xFF000000, 0xFF5ea618)); the progressBar color is green, but I can't see it moving. – hico Jun 11 '12 at 10:32
  • If you want full control you should create the graphics of the progress bar yourself. – Moritz Jun 11 '12 at 18:55
  • 1
    tints both background and foreground – desgraci Sep 30 '16 at 01:43
  • 'Drawable.SetColorFilter(Color, PorterDuff.Mode)' is obsolete: 'deprecated' – Luca Ziegler Jan 12 '21 at 09:32
15

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);   
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
10

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
Merthan Erdem
  • 5,598
  • 2
  • 22
  • 29
8

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

It only works above API 21

Daniel Park
  • 141
  • 1
  • 2
  • 3
3

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.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
1

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.

Gene
  • 10,819
  • 1
  • 66
  • 58
0

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

cutiko
  • 9,887
  • 3
  • 45
  • 59
0

Try this:

progress_wheel.getIndeterminateDrawable().setColorFilter(Color.parseColor(getPreferences().getString(Constant.SECOND_COLOR, Constant.SECONDARY_COLOR)), android.graphics.PorterDuff.Mode.MULTIPLY);
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
-1

Try this Code:

   CircularProgressIndicator  circularProgressIndicator = findViewById(R.id.tenant_progress_color);
    circularProgressIndicator.setIndicatorColor(color);
    circularProgressIndicator.setTrackColor(R.color.gray);
Manideep
  • 353
  • 3
  • 13