593

I'm using an horizontal progress bar in my Android application, and I want to change its progress color (which is Yellow by default). How can I do it using code (not XML)?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
WhiteTigerK
  • 6,873
  • 5
  • 21
  • 13
  • Have you tried MyProgressBar.setProgressDrawable(Drawable d) specifying a bitmap with the color you want? http://developer.android.com/reference/android/widget/ProgressBar.html#setProgressDrawable(android.graphics.drawable.Drawable) http://developer.android.com/reference/android/graphics/drawable/Drawable.html – fupsduck Jan 07 '10 at 14:25
  • 2
    Yes i've tried it but it doesn't work. It sets the background color of the whole progress bar view instead of setting the background color of only the bar itself. Thanks. – WhiteTigerK Jan 07 '10 at 14:41
  • 74
    `android:indeterminateTint="@android:color/white"` works only on API >=21 – Madeyedexter Aug 19 '17 at 17:08
  • 1
    `android:progressTint="@android:color/white"` when you use non-indeterminate – Kamil Sep 26 '22 at 15:05

42 Answers42

540

For a horizontal ProgressBar, you can use a ColorFilter, too, like this:

progressBar.getProgressDrawable().setColorFilter(
    Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);

Red ProgressBar using color filter

Note: This modifies the appearance of all progress bars in your app. To only modify one specific progress bar, do this:

Drawable progressDrawable = progressBar.getProgressDrawable().mutate();
progressDrawable.setColorFilter(Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
progressBar.setProgressDrawable(progressDrawable);

If progressBar is indeterminate then use getIndeterminateDrawable() instead of getProgressDrawable().

Since Lollipop (API 21) you can set a progress tint:

progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));

Red ProgressBar using progress tint

Torben Kohlmeier
  • 6,713
  • 1
  • 15
  • 15
  • 7
    I wanted to use this answer as it seemed to me to be the simplest one but my progress bar is completely red independently of the progress value. Are you sure of the Mode to use? – L. G. May 29 '13 at 13:55
  • I justed tested it again, here it works perfectly. Did you set a progress? Maybe you only see the light red background. – Torben Kohlmeier May 29 '13 at 14:11
  • Yes I set a progress, I had to set a color with transparency for this to work. I tested it on android 2.2, not sure if it could have an influence. – L. G. May 29 '13 at 14:46
  • 36
    @resus I have the same problem, the bar was fully red and no progress was visible. But I solved it changing Mode.SRC_IN to Mode.MULTIPLY. – Derzu Jul 02 '13 at 15:00
  • 43
    Don't forget to do the same for `getIndeterminateDrawable` if you are using indeterminate progress. – Thommy Sep 18 '14 at 06:45
  • 3
    Also note that doing this will modify drawables of ALL progress bars in your app. To do this only to a particular one, call mutate() on drawable, then set color filter on it and then call set*Drawable on your progressBar. – dimsuz Feb 19 '15 at 11:13
  • 9
    how to don't change background color ? I just want to change progress color. – kangear Feb 25 '15 at 02:42
  • 1
    That's pretty neat. How to change RED to another custom color like #12ddfe – TheOnlyAnil Mar 07 '15 at 19:12
  • 3
    You can use Color.parseColor("#12ddfe") instead of Color.RED. – Torben Kohlmeier Mar 10 '15 at 06:59
  • Great it work onward 2.3 For indeterminate progress bar i am using **getIndeterminateDrawable** – Ram Apr 07 '15 at 09:15
  • 2
    Awesome suggestion! `progressBar.getProgressDrawable().setColorFilter(Color.RED, Mode.SRC_IN);` works for me without modification. And with mode `MULTIPLY` progress drawable turns a couple of shades darker. – dominus Jun 04 '15 at 07:42
  • for a circle one? – user25 Sep 16 '16 at 19:22
  • @TorbenKohlmeier Since this is the most popular answer, I think it would be very helpful if you added "@dimsuz"s warning that this will modify drawables of all progress bars and one should use mutate to prevent that. – John L. Dec 21 '17 at 08:08
  • Not that you must call `setProgress()` **after** setting the tint, otherwise you only get an empty progress bar. At least this is true on Android 4.4.4. – Magnus Sep 13 '18 at 17:37
  • All what I need, I only added `Color.parseColor(myColor)` to use a color from a `String` stored on database – IgniteCoders Nov 04 '19 at 12:08
  • the method `setColorFilter` is deprecated from API 29. To resolve follow this link [https://stackoverflow.com/a/56717316/3122668](https://stackoverflow.com/a/56717316/3122668) as suggest @shmakova – capo11 Apr 09 '20 at 06:50
535

This is not programmatically but I think it could help a lot of people anyway.
I tried a lot and the most efficient way was to add this lines to my ProgressBar in the .xml File:

            android:indeterminate="true"
            android:indeterminateTintMode="src_atop"
            android:indeterminateTint="@color/secondary"

So in the end this code did it for me:

<ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="50dp"
            android:layout_marginBottom="50dp"
            android:visibility="visible"
            android:indeterminate="true"
            android:indeterminateTintMode="src_atop"
            android:indeterminateTint="@color/secondary">

This solution works for API 21+

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
FFirmenich
  • 5,611
  • 1
  • 19
  • 28
316

I'm sorry that it's not the answer, but what's driving the requirement setting it from code ? And .setProgressDrawable should work if it's defined correctly

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@android:id/background">
    <shape>
        <corners android:radius="5dip" />
        <gradient
                android:startColor="#ff9d9e9d"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.75"
                android:endColor="#ff747674"
                android:angle="270"
        />
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#80ffd300"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:angle="270"
            />
        </shape>
    </clip>
</item>

<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners
                android:radius="5dip" />
            <gradient
                android:startColor="@color/progress_start"
                android:endColor="@color/progress_end"
                android:angle="270" 
            />
        </shape>
    </clip>
</item>

</layer-list>
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Alex Volovoy
  • 67,778
  • 13
  • 73
  • 54
  • 6
    The reason is because I'm creating the progress bar dynamically, and setting its color upon user request. Since I usually use code for building my GUI screen and components, I'm not familiar with the attached XML and I don't know what's a layer-list (although I'm guessing that you are building the progress bar based on several layers..). In case I do want to use the XML you've attached - where should I place the it in the project's folder and is there anything more I need to do for creating a progress bar based on the XML settings ? Thanks. – WhiteTigerK Jan 07 '10 at 15:33
  • 1
    You save this xml as a file and put it in the drawable folder ( let's say my_progress.xml ) than you set it as a drawable in MyProgressBar.setProgressDrawable() To change colors - you'll need to change those values in @color/progress_start @color/progress_end It's basically a gradient and you can put hex in there. – Alex Volovoy Jan 07 '10 at 15:50
  • 3
    Note - file is the copy of the one that's in SDK. I've dropped copyright here. If you look in the res/drawable folder you'll see exactly what i've posted - colors are set to the yellow gradient, instead of custom colors. – Alex Volovoy Jan 07 '10 at 15:53
  • 7
    Its does not show any color change to me. please tell the color which worked out. – Praveen Jul 02 '10 at 11:09
  • Hi Alex , your answers is very nice. but, i am tried to change custom progress wheel color at runtime. but, i could not. please help me to solve my issue.. after progress reaches100%, then that time change the color and start progress..i download code from this..http://stackoverflow.com/questions/18503718/color-changing-in-custom-progress-wheel-at-runtime-in-android-programmatically – harikrishnan Aug 29 '13 at 11:20
  • can we adjust background width of seekbar ? – Vaishali Sutariya Sep 13 '14 at 05:54
  • Just put these all in a `` and this works perfect – Aman Alam Dec 24 '14 at 14:18
  • If we would like to perform operations on click of each color of a progress bar. what we can do? – Anshul Tyagi Nov 18 '15 at 13:20
  • @AlexVolovoy I know it's been a while but could you add the folder where you took the file from? Was it this one `%SDK-FOLDER%/platforms/android-*/data/res/drawable/progress_horizontal.xml` – Bruno Bieri Feb 17 '20 at 09:07
  • If you want round `progressIndicators` as well just change `` to `` Enjoy! – Prince Nov 02 '20 at 11:11
  • Hi, I think Alex' solution is correct answer. I checked https://www.geeksforgeeks.org/how-to-change-the-progressbar-color-in-android/ and see same solution. I used it. – Ali Hosein pour Apr 11 '22 at 19:19
245

For my indeterminate progressbar (spinner) I just set a color filter on the drawable. Works great and just one line.

Example where setting color to red:

ProgressBar spinner = new android.widget.ProgressBar(
                context,
                null,
                android.R.attr.progressBarStyle);

spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);

enter image description here

jhavatar
  • 3,236
  • 1
  • 18
  • 11
  • 17
    I like your answer the best. For mine to work though I had to do this: myProgressBarSpinner.getIndeterminateDrawable().setColorFilter(new LightingColorFilter(0xFF000000, foregroundColorDesired)); – Art Geigel Jul 01 '13 at 00:38
  • 5
    Also note that doing this will modify drawables of ALL progress bars in your app. To do this only to a particular one, call mutate() on drawable, then set color filter on it and then call setIntederminateDrawable on your progressBar. – dimsuz Feb 19 '15 at 11:13
  • 3
    NB: Those who use this and wondering why their colour doesn't look exactly as the hex string they put in, it has a Multiple Colour filter on it. Change `android.graphics.PorterDuff.Mode.MULTIPLY` to `PorterDuff.Mode.SRC_IN` and you will get the exact hex colour. – micnguyen Aug 17 '15 at 07:38
  • I think he just need this answer ..... android:indeterminateTint="@android:color/black" – Muhammad Adil May 23 '16 at 17:25
  • 4
    i used as : progressBar= (ProgressBar) findViewById(R.id.progressBar); progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(this,android.R.color.white), android.graphics.PorterDuff.Mode.MULTIPLY); – ashishdhiman2007 Jun 17 '17 at 12:35
218

This is an old question, but using theme is not mentioned here. If your default theme is using AppCompat, your ProgressBar's color will be colorAccent you have defined.

Changing colorAccent will also change your ProgressBar's color, but there changes also reflects at multiple places. So, if you want a different color just for a specific PregressBar you can do that by applying theme to that ProgressBar :

  • Extend your default theme and override colorAccent

    <style name="AppTheme.WhiteAccent">
        <item name="colorAccent">@color/white</item> <!-- Whatever color you want-->
    </style>
    
  • And in ProgressBar add the android:theme attribute:

    android:theme="@style/AppTheme.WhiteAccent"
    

So it will look something like this:

<ProgressBar
        android:id="@+id/loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dp"
        android:theme="@style/AppTheme.WhiteAccent" />

So you are just changing a colorAccent for your particular ProgressBar.

Note: Using style will not work. You need to use android:theme only. You can find more use of theme here: https://plus.google.com/u/0/+AndroidDevelopers/posts/JXHKyhsWHAH

kirtan403
  • 7,293
  • 6
  • 54
  • 97
  • 20
    finally! thanks, it seems like not many developers use AppCompat, many users just vote-ups answers with solution for API 21+, not good at all, they should support more devices/OS as possible – user25 Sep 16 '16 at 19:37
  • 1
    Awesome! Worked for me, but I had to make sure there was a parent="@style/Theme.AppCompat" somewhere up the style hierarchy I had another parent and it didn't work at first. – Nublodeveloper Jan 24 '17 at 10:09
  • @Nublodeveloper Yes I mentioned that in the first line if you have noticed. – kirtan403 Jan 24 '17 at 12:36
  • 1
    @kirtan403 Ah, yes, I get it now you mention it, but I didn't understand it that way when I first read it. I read it too fast and I was looking for code. Thanks anyway, your answer is the best for me! – Nublodeveloper Jan 24 '17 at 13:57
  • +1 Liked your solution. But how to set the color of the theme in java??? For example if I want it to be red #ff5900 – Maseed Jan 05 '18 at 12:14
  • @kirtan403 is there any way to change 'colorAccent' of ProgressBar value programetically? – Hiren Dabhi Apr 11 '18 at 09:31
  • how to horizantal progressbar? – Ucdemir May 02 '18 at 15:01
  • Am i wrong, or does this too work only for API 21+ ? – Kirill Starostin Jul 31 '18 at 10:28
  • Thanks, buddy! This is exactly what I was looking for – bellotas Dec 03 '19 at 21:54
69

All API

if use all API just create the theme in style

style.xml

<resources>

    //...

    <style name="progressBarBlue" parent="@style/Theme.AppCompat">
        <item name="colorAccent">@color/blue</item>
    </style>

</resources>

and use in progress

<ProgressBar
    ...
    android:theme="@style/progressBarBlue" />

API level 21 and higher

if used in API level 21 and higher just use this code:

<ProgressBar
   //...
   android:indeterminate="true"
   android:indeterminateTintMode="src_atop"
   android:indeterminateTint="@color/secondary"/>
Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78
53

This works for me. It also works for lower version too. Add this to your syles.xml

<style name="ProgressBarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="colorAccent">@color/colorPrimary</item>
</style>

And use it like this in xml

<ProgressBar
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:theme="@style/ProgressBarTheme"
   />
Devinder Jhinjer
  • 645
  • 7
  • 13
41

This worked for me :

<ProgressBar
 android:indeterminateTint="#d60909"
 ... />
MOH3N
  • 905
  • 9
  • 14
34

as per some of the suggestions, you CAN specify a shape and clipdrawable with a colour, then set it. I have this working programatically. This is how I do it..

First make sure you import the drawable library..

import android.graphics.drawable.*;

Then use the code similar to below;

ProgressBar pg = (ProgressBar)row.findViewById(R.id.progress);
final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null,null));
String MyColor = "#FF00FF";
pgDrawable.getPaint().setColor(Color.parseColor(MyColor));
ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
pg.setProgressDrawable(progress);   
pg.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal));
pg.setProgress(45);
PaulieG
  • 341
  • 3
  • 2
  • 1
    I tried the code above, unfortunately it just results in an "empty" progress bar. Am I missing something? – OceanBlue May 17 '11 at 19:41
  • You need to call setLevel on the ClipDrawable. It takes a value from 0 to 10000. So, progress.setLevel(2500) would be 25% full. – HappyEngineer Sep 19 '11 at 02:08
  • 1
    I have submitted an edit to the answer that explains one reason for an "empty" progress bar - and 2 ways to fix it (incl @HappyEngineer's fix) – Richard Le Mesurier Feb 22 '12 at 07:30
  • @RichardLeMesurier can you post the explanation + 2 ways in the comment section? `progress.setLevel(2500)` doesn't work for me and apparently your edit wasn't accepted for some reason. Thanks. +1. – ateiob Jun 29 '12 at 13:29
  • @ateiob I have written [a new answer to explain...](http://stackoverflow.com/a/11288434/383414) – Richard Le Mesurier Jul 02 '12 at 06:02
  • In order to replace deprecated methods in the last line: pg.setBackground(ContextCompat.getDrawable(getActivity(), android.R.drawable.progress_horizontal)); – Gustavo Pagani Apr 15 '16 at 09:42
31

if Indeterminate:

((ProgressBar)findViewById(R.id.progressBar))
    .getIndeterminateDrawable()
    .setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
Paweł Tomkiel
  • 1,974
  • 2
  • 21
  • 39
the.knife
  • 419
  • 4
  • 3
26

Trust me, the easiest solution is just paste this inside progressBar :

android:indeterminateTint="@android:color/white"
AI Shakil
  • 1,015
  • 10
  • 20
25

Nowadays in 2016 I found some pre-Lollipop devices don't honour the colorAccent setting, so my final solution for all APIs is now the following:

// fixes pre-Lollipop progressBar indeterminateDrawable tinting
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {

    Drawable wrapDrawable = DrawableCompat.wrap(mProgressBar.getIndeterminateDrawable());
    DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(getContext(), android.R.color.holo_green_light));
    mProgressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
} else {
    mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), android.R.color.holo_green_light), PorterDuff.Mode.SRC_IN);
}

For bonus points, it doesn't use any deprecated code. Try it!

Henrique de Sousa
  • 5,727
  • 49
  • 55
  • Don't forget to call `wrapDrawable.mutate()` with `DrawableCompat.setTint(wrapDrawable.mutate(), ContextCompat.getColor(getContext(), android.R.color.holo_green_light));` – Tarsem Singh Aug 24 '18 at 11:21
  • Just tried it (again) on a fresh new 4.4.2 emulator and it works without any issues whatsoever, check your `.xml` settings and also if something else is overriding the `ProgressBar`color. – Henrique de Sousa Feb 05 '19 at 14:49
25
android:progressTint="#ffffff" 
Paul
  • 823
  • 1
  • 11
  • 18
25

For SDK ver 21 and above

android:indeterminateTint="@color/orange"

in XML Works for me, is easy enough.

Mughil
  • 705
  • 7
  • 8
  • If you want to change for the whole app, kindly change the value of #FE6C27. in your Values/colors.xml. – Mughil May 30 '17 at 12:25
24

THis is what i did. Worked.

ProgressBar:

<ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:indeterminateDrawable="@drawable/progressdrawable"
           />

progressdrawable.xml:
Here use gradient to change colour as you like. And android:toDegrees="X" increse the value of X and progressbar rotate fast. Decrease and it rotate slow.Customize according to your needs.

<?xml version="1.0" encoding="utf-8"?>
     <rotate xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="4000"
            android:fromDegrees="0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="360" >

            <shape
                android:innerRadius="20dp"
                android:shape="ring"
                android:thickness="4dp"
                android:useLevel="false" >
                <size
                    android:height="48dp"
                    android:width="48dp" />

                <gradient
                    android:centerColor="#80ec7e2a"
                    android:centerY="0.5"
                    android:endColor="#ffec7e2a"
                    android:startColor="#00ec7e2a"
                    android:type="sweep"
                    android:useLevel="false" />
            </shape>

        </rotate>

sample: enter image description here

Debasish Ghosh
  • 1,867
  • 20
  • 29
22

Add in ProgressBar inside of Xml

For SDK ver 21 and above

android:indeterminateTint="@color/red"
Mark Nashat
  • 668
  • 8
  • 9
20

Hit the same problem while working on modifying the look/feel of the default progress bar. Here is some more info that will hopefully help people :)

  • The name of the xml file must only contain characters: a-z0-9_. (ie. no capitals!)
  • To reference your "drawable" it is R.drawable.filename
  • To override the default look, you use myProgressBar.setProgressDrawable(...), however you need can't just refer to your custom layout as R.drawable.filename, you need to retrieve it as a Drawable:
    Resources res = getResources();
    myProgressBar.setProgressDrawable(res.getDrawable(R.drawable.filename);
    
  • You need to set style before setting progress/secondary progress/max (setting it afterwards for me resulted in an 'empty' progress bar)
pyko
  • 3,399
  • 5
  • 26
  • 31
19

You can try to change your Styles, Themes, or using android:indeterminateTint="@color/yourColor" anywhere you want, but there's just one way o doing that will work on any Android SKD version:

If you progress bar is not indeterminate, please use:

progressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(context, R.color.yourColor), PorterDuff.Mode.SRC_IN );

If you progress bar is indeterminate, please use:

progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.yourColor), PorterDuff.Mode.SRC_IN );

It's sad that Android is such a mess!

  • Thanks! It works for indeterminate `ProgressBar`, but for determinate it paints whole scale with color. – CoolMind Sep 28 '18 at 11:29
17

For default ( indeterminate )

add

android:indeterminateTint="@color/white"

for determinate

    android:progressTint="@color/color_1"

    //OR
    progressBar.getProgressDrawable().setColorFilter( PorterDuffColorFilter(Color.RED,android.graphics.PorterDuff.Mode.SRC_IN));
Nasib
  • 1,173
  • 1
  • 13
  • 23
16

How I did it in horizontal ProgressBar:

    LayerDrawable layerDrawable = (LayerDrawable) progressBar.getProgressDrawable();
    Drawable progressDrawable = layerDrawable.findDrawableByLayerId(android.R.id.progress);
    progressDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
mieszk3
  • 276
  • 3
  • 9
  • 2
    I did the same thing, plus the background color change: layerDrawable.findDrawableByLayerId(android.R.id.background).setColorFilter(trackColor,PorterDuff.Mode.SRC); – Kamen Dobrev Jan 06 '16 at 14:08
  • 2
    This answer along with @KamenDobrev 's comment is the only one that actually did what I wanted. When I only set a color filter to the progress drawable the background color changed as well. – m_katsifarakis Jul 10 '16 at 07:16
15

There's probably one thing that hasn't been referred to in this answer:

If your theme is inheriting from Theme.AppCompat, ProgressBar will assume the color you defined as "colorAccent" in your theme.

So, using..

<item name="colorAccent">@color/custom_color</item>

..will tint the color of the ProgressBar automagically to the @color/custom_color .

Henrique de Sousa
  • 5,727
  • 49
  • 55
15

Simplest Solution if you want to change the colour in the layout xml file, use the below code and use indeterminateTint property for your desired color.

    <ProgressBar
      android:id="@+id/progressBar"
      style="?android:attr/progressBarStyle"
      android:layout_width="wrap_content"
      android:indeterminate="true"
      android:indeterminateTintMode="src_atop"
      android:indeterminateTint="#ddbd4e"
      android:layout_height="wrap_content"
      android:layout_marginBottom="20dp"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true" />
Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
14

The most simple way of changing the foreground and background colour of a progress bar is

<ProgressBar
                        style="@android:style/Widget.ProgressBar.Horizontal"
                        android:id="@+id/pb_main"
                        android:layout_width="match_parent"
                        android:layout_height="8dp"
                        android:progress="30"
                        android:progressTint="#82e9de"
                        android:progressBackgroundTint="#82e9de"
                        />

just add

                        android:progressTint="#82e9de" //for foreground colour
                        android:progressBackgroundTint="#82e9de" //for background colour
Satyam Patil
  • 161
  • 1
  • 6
13

This solution worked for me :

<style name="Progressbar.White" parent="AppTheme">
    <item name="colorControlActivated">@color/white</item>
</style>

<ProgressBar
    android:layout_width="@dimen/d_40"
    android:layout_height="@dimen/d_40"
    android:indeterminate="true"
    android:theme="@style/Progressbar.White"/>
Horai Nuri
  • 5,358
  • 16
  • 75
  • 127
S.Javed
  • 383
  • 3
  • 8
10

To change horizontal ProgressBar color (in kotlin):

fun tintHorizontalProgress(progress: ProgressBar, @ColorInt color: Int = ContextCompat.getColor(progress.context, R.color.colorPrimary)){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        progress.progressTintList = ColorStateList.valueOf(color)
    } else{
        val layerDrawable = progress.progressDrawable as? LayerDrawable
        val progressDrawable = layerDrawable?.findDrawableByLayerId(android.R.id.progress)
        progressDrawable?.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
    }
}

To change indeterminate ProgressBar color:

fun tintIndeterminateProgress(progress: ProgressBar, @ColorInt color: Int = ContextCompat.getColor(progress.context, R.color.colorPrimary)){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        progress.indeterminateTintList = ColorStateList.valueOf(color)
    } else {
        (progress.indeterminateDrawable as? LayerDrawable)?.apply {
            if (numberOfLayers >= 2) {
                setId(0, android.R.id.progress)
                setId(1, android.R.id.secondaryProgress)
                val progressDrawable = findDrawableByLayerId(android.R.id.progress).mutate()
                progressDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
            }
        }
    }
}

And it finally normally tint pre-lollipop progressBars

tinted progress on api 19

Community
  • 1
  • 1
John
  • 1,447
  • 15
  • 16
10

Simply use:

DrawableCompat.setTint(progressBar.getIndeterminateDrawable(),yourColor)
Amir Hossein Ghasemi
  • 20,623
  • 10
  • 57
  • 53
9

One more little thing, the theme solution does work if you inherit a base theme, so for app compact your theme should be:

<style name="AppTheme.Custom" parent="@style/Theme.AppCompat">
    <item name="colorAccent">@color/custom</item>
</style>

And then set this in the progress bar theme

<ProgressBar
    android:id="@+id/progressCircle_progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:theme="@style/AppTheme.Custom"
    android:indeterminate="true"/>
Calin
  • 6,661
  • 7
  • 49
  • 80
8

simply use:

PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
    mode = PorterDuff.Mode.MULTIPLY;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
    progressBar.setProgressBackgroundTintList(ColorStateList.valueOf(Color.RED));
} else {
    Drawable progressDrawable;
    progressDrawable = (progressBar.isIndeterminate() ? progressBar.getIndeterminateDrawable() : progressBar.getProgressDrawable()).mutate();
    progressDrawable.setColorFilter(context.getResources().getColor(Color.RED), mode);
    progressBar.setProgressDrawable(progressDrawable);
}
Mohammad Fneish
  • 829
  • 1
  • 9
  • 20
7

Horizontal progress bar custom material style :

To change color of background and progress of horizontal progress bar.

<style name="MyProgressBar" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
    <item name="android:progressBackgroundTint">#69f0ae</item>
    <item name="android:progressTint">#b71c1c</item>
    <item name="android:minWidth">200dp</item>
</style>

Apply it to progress bar by setting style attribute, for custom material styles and custom progress bar check http://www.zoftino.com/android-progressbar-and-custom-progressbar-examples

Arnav Rao
  • 6,692
  • 2
  • 34
  • 31
7

Use the android.support.v4.graphics.drawable.DrawableCompat:

            Drawable progressDrawable = progressBar.getIndeterminateDrawable();
            if (progressDrawable  != null) {
                Drawable mutateDrawable = progressDrawable.mutate();
                DrawableCompat.setTint(mutateDrawable, primaryColor);
                progressBar.setProgressDrawable(mutateDrawable);
            }
Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
6

Posted to add info about PaulieG's answer, since ateiob asked me to explain something...


I can say that there is (or at least was, at the time of writing when I looked at that current version of Android source code) a bug/issue/optimisation in the ProgressBar code that ignores an attempt to set the progress to a value it is already at.

  • i.e. if progress = 45, and you try to set it to 45, the code will do nothing, and will not redraw the progress.

After calling ProgressBar.setProgressDrawable(), your progress bar will be blank (because you changed the drawable part).

This means you need to set the progress, and redraw it. But if you just set the progress to a preserved value, it will do nothing.

You must set it to 0 first, then to the "old" value again, and the bar will redraw.


So to summarise:

  • preserve the "old" progress value
  • update the drawable / colour (makes bar blank)
  • reset the progress to 0 (otherwise next line does nothing)
  • reset the progress to the "old" value (fixes bar)
  • invalidate

Below is a method I have that does this:

protected void onResume()
{
    super.onResume();
    progBar = (ProgressBar) findViewById(R.id.progress_base);

    int oldProgress = progBar.getProgress();

    // define new drawable/colour
    final float[] roundedCorners = new float[]
        { 5, 5, 5, 5, 5, 5, 5, 5 };
    ShapeDrawable shape = new ShapeDrawable(new RoundRectShape(
        roundedCorners, null, null));
    String MyColor = "#FF00FF";
    shape.getPaint().setColor(Color.parseColor(MyColor));
    ClipDrawable clip = new ClipDrawable(shape, Gravity.LEFT,
        ClipDrawable.HORIZONTAL);
    progBar.setProgressDrawable(clip);

    progBar.setBackgroundDrawable(getResources().getDrawable(
        android.R.drawable.progress_horizontal));

    // work around: setProgress() ignores a change to the same value
    progBar.setProgress(0);
    progBar.setProgress(oldProgress);

    progBar.invalidate();
}

As far as HappyEngineer's solution, I think it was a similar workaround, to manually set the "progress" offset. In either case, the above code should work for you.

Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
  • Thanks..this works indeed..but as my drawable was static (from resources)..it didn't work. It only worked after i added "drawable.invalidateSelf();" before the 'setProgress(0)' and 'setProgressDrawable(drawable)' – Alécio Carvalho Feb 13 '15 at 13:43
5

If any one want to change color programmatically:

progressBar.progressTintList = ColorStateList.valueOf(Color.RED)
4

For a horizontal style ProgressBar I use:

import android.widget.ProgressBar;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ClipDrawable;
import android.view.Gravity;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;

public void setColours(ProgressBar progressBar,
                        int bgCol1, int bgCol2, 
                        int fg1Col1, int fg1Col2, int value1,
                        int fg2Col1, int fg2Col2, int value2)
  {
    //If solid colours are required for an element, then set
    //that elements Col1 param s the same as its Col2 param
    //(eg fg1Col1 == fg1Col2).

    //fgGradDirection and/or bgGradDirection could be parameters
    //if you require other gradient directions eg LEFT_RIGHT.

    GradientDrawable.Orientation fgGradDirection
        = GradientDrawable.Orientation.TOP_BOTTOM;
    GradientDrawable.Orientation bgGradDirection
        = GradientDrawable.Orientation.TOP_BOTTOM;

    //Background
    GradientDrawable bgGradDrawable = new GradientDrawable(
            bgGradDirection, new int[]{bgCol1, bgCol2});
    bgGradDrawable.setShape(GradientDrawable.RECTANGLE);
    bgGradDrawable.setCornerRadius(5);
    ClipDrawable bgclip = new ClipDrawable(
            bgGradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);     
    bgclip.setLevel(10000);

    //SecondaryProgress
    GradientDrawable fg2GradDrawable = new GradientDrawable(
            fgGradDirection, new int[]{fg2Col1, fg2Col2});
    fg2GradDrawable.setShape(GradientDrawable.RECTANGLE);
    fg2GradDrawable.setCornerRadius(5);
    ClipDrawable fg2clip = new ClipDrawable(
            fg2GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);        

    //Progress
    GradientDrawable fg1GradDrawable = new GradientDrawable(
            fgGradDirection, new int[]{fg1Col1, fg1Col2});
    fg1GradDrawable.setShape(GradientDrawable.RECTANGLE);
    fg1GradDrawable.setCornerRadius(5);
    ClipDrawable fg1clip = new ClipDrawable(
            fg1GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);        

    //Setup LayerDrawable and assign to progressBar
    Drawable[] progressDrawables = {bgclip, fg2clip, fg1clip};
    LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);     
    progressLayerDrawable.setId(0, android.R.id.background);
    progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
    progressLayerDrawable.setId(2, android.R.id.progress);

    //Copy the existing ProgressDrawable bounds to the new one.
    Rect bounds = progressBar.getProgressDrawable().getBounds();
    progressBar.setProgressDrawable(progressLayerDrawable);     
    progressBar.getProgressDrawable().setBounds(bounds);

    // setProgress() ignores a change to the same value, so:
    if (value1 == 0)
        progressBar.setProgress(1);
    else
        progressBar.setProgress(0);
    progressBar.setProgress(value1);

    // setSecondaryProgress() ignores a change to the same value, so:
    if (value2 == 0)
        progressBar.setSecondaryProgress(1);
    else
        progressBar.setSecondaryProgress(0);
    progressBar.setSecondaryProgress(value2);

    //now force a redraw
    progressBar.invalidate();
  }

An example call would be:

  setColours(myProgressBar, 
          0xff303030,   //bgCol1  grey 
          0xff909090,   //bgCol2  lighter grey 
          0xff0000FF,   //fg1Col1 blue 
          0xffFFFFFF,   //fg1Col2 white
          50,           //value1
          0xffFF0000,   //fg2Col1 red 
          0xffFFFFFF,   //fg2Col2 white
          75);          //value2

If you don't need the 'secondary progress' simply set value2 to value1.

Keith
  • 646
  • 6
  • 4
4

Apply this custom style to the progress bar.

<style name="customProgress" parent="@android:style/Widget.ProgressBar.Small">
        <item name="android:indeterminateDrawable">@drawable/progress</item>
        <item name="android:duration">40</item>
        <item name="android:animationCache">true</item>
        <item name="android:drawingCacheQuality">low</item>
        <item name="android:persistentDrawingCache">animation</item>
    </style>

@drawable/progress.xml -

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/spinner_white"
    android:pivotX="50%"
    android:pivotY="50%" />

Use this type of image for progress bar. enter image description here

For better result you can use multiple progress images. And please don't hesitate using images, because Android Platform itself use images for progressbar. The code is extracted from sdk :)

Neo
  • 3,546
  • 1
  • 24
  • 31
4

Here is the code for changing the color of ProgressBar by programatically.

ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb_listProgressBar);
int colorCodeDark = Color.parseColor("#F44336");
progressBar.setIndeterminateTintList(ColorStateList.valueOf(colorCodeDark));
Naveen Kumar M
  • 7,497
  • 7
  • 60
  • 74
4
ProgressBar freeRamPb = findViewById(R.id.free_ram_progress_bar);

freeRamPb.getProgressDrawable().setColorFilter(
Color.BLUE, android.graphics.PorterDuff.Mode.SRC_IN);
3
ProgressBar bar;

private Handler progressBarHandler = new Handler();

GradientDrawable progressGradientDrawable = new GradientDrawable(
        GradientDrawable.Orientation.LEFT_RIGHT, new int[]{
                0xff1e90ff,0xff006ab6,0xff367ba8});
ClipDrawable progressClipDrawable = new ClipDrawable(
        progressGradientDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
Drawable[] progressDrawables = {
        new ColorDrawable(0xffffffff),
        progressClipDrawable, progressClipDrawable};
LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);


int status = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO Auto-generated method stub
    setContentView(R.layout.startup);

    bar = (ProgressBar) findViewById(R.id.start_page_progressBar);
    bar.setProgress(0);
    bar.setMax(100);

    progressLayerDrawable.setId(0, android.R.id.background);
    progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
    progressLayerDrawable.setId(2, android.R.id.progress);

    bar.setProgressDrawable(progressLayerDrawable);
}

This helped me to set a custom color to progressbar through code. Hope it helps

SKT
  • 1,821
  • 1
  • 20
  • 32
3

It is so simple using attr , if you are dealing with multistyle apps:

try this way:

Declare below attribute attrs.xml

 <attr name="circularProgressTheme" format="reference"></attr>

Paste below code in styles.xml

 <style name="ProgressThemeWhite" parent="ThemeOverlay.AppCompat.Light">
        <item name="colorAccent">#FF0000</item>
    </style>

    <style name="circularProgressThemeWhite">
        <item name="android:theme">@style/ProgressThemeWhite</item>
    </style>


  <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">

   <item name="circularProgressTheme">@style/circularProgressThemeWhite</item>

 </style>

use progress bar like below

  <ProgressBar
        style="?attr/circularProgressTheme"
        android:id="@+id/commonProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="visible"/>
DeepakPanwar
  • 1,389
  • 14
  • 22
2

Horizontal ProgressBar uses rectangle shape drawable for background, ClipDrawable constructed from rectangle shape for progresses (primary & secondary). Tinting changes colors to some tint. If you want targeted colors for all three separately then you can use ProgressBar.setProgressDrawable() as following:

    LayerDrawable progressBarDrawable = new LayerDrawable(
        new Drawable[]{
                new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                            new int[]{Color.parseColor("#ff0000ff"),Color.parseColor("#ff0000ff")}),

                new ClipDrawable(
                            new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                    new int[]{Color.parseColor("#ff00ff00"),Color.parseColor("#ff00ff00")}),
                            Gravity.START,
                            ClipDrawable.HORIZONTAL),

                new ClipDrawable(
                        new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                new int[]{Color.parseColor("#ffff0000"),Color.parseColor("#ffff0000")}),
                        Gravity.START,
                        ClipDrawable.HORIZONTAL)
            });

    progressBarDrawable.setId(0,android.R.id.background);
    progressBarDrawable.setId(1,android.R.id.secondaryProgress);
    progressBarDrawable.setId(2,android.R.id.progress);
    ((ProgressBar)findViewById(R.id.progressBar)).setProgressDrawable(progressBarDrawable);

Note that order of drawable layers is important while initializing LayerDrawable. First drawable should be for background. As per my experiment switching ids does not work. If you set padding to progressbar then this approach will not work. If you need padding then you can use a container for the progressbar such as a LinearLayout.

Birendra Singh
  • 842
  • 11
  • 19
2

Since method public void setColorFilter(@ColorInt int color, @NonNull PorterDuff.Mode mode) is deprecated, I use such form:

progressBar.indeterminateDrawable.colorFilter =
            PorterDuffColorFilter(ContextCompat.getColor(this, R.color.black), PorterDuff.Mode.SRC_IN)
Andrii Turkovskyi
  • 27,554
  • 16
  • 95
  • 105
0

It seems that an indeterminate linear progress indicator can be made multicolor in newer versions of material design library.

Making animation type of the progress bar contiguous achieves this.

  • In the layout with this attribute:
    app:indeterminateAnimationType
    
  • or programmatically with this method:
    setIndeterminateAnimationType()
    

Refer here for more info.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
0

I know I am late but this is the easiest solution if you only want to change the color of your progress bar, In your theme, simply add,

@color/your_color

colorSecondary will change you progress color throughout.