21

I have a progressbar with the following style: style="?android:attr/android:progressBarStyleSmall"

Sadly the bar is nearly white and in my case displayed on a white background. The progressbar is nearly invisible because of that.

How can I change the color of the progressbar? A darker grey would be great.

Janusz
  • 187,060
  • 113
  • 301
  • 369

7 Answers7

41
progressBar.getIndeterminateDrawable().setColorFilter(
            getResources().getColor(R.color.light_light_purple),
            android.graphics.PorterDuff.Mode.SRC_IN);

This code changes the default holo inderminate drawable color to your own color. Define your color code and replace R.color.light_light_purple to R.color.your_color_code.

Vishal Pandey
  • 696
  • 8
  • 7
  • 1
    This should really be the accepted answer. I only recently discovered the color filter method of basically changing anything to whatever color you want, and it's pretty awesome. – LukeWaggoner Dec 19 '14 at 15:27
  • Thanks a lot..!Plzz accept my answer for more benefit to others :) – Vishal Pandey Mar 31 '15 at 07:06
  • I only needed to use this snippet for SDK versions prior to Lollipop, So, I wrapped this snippet in a static method: `LegacyStyleUtil#adjustLegacyStyle(Context, ProgressBar)`. Then, I invoke this method from my activities/fragments. `public static void adjustLegacyStyle(Context context, ProgressBar progressBar) { if (BuildConfigUtil.isLegacySDK() && progressBar != null) { progressBar.getIndeterminateDrawable().setColorFilter( context.getResources().getColor(R.color.green_progress), PorterDuff.Mode.SRC_IN); } }` – Andy H. Jun 15 '15 at 13:06
  • 3
    Does not work on newer Android, where the indeterminate progress bar is simple circle: it stops turning around. – Oliv Aug 25 '15 at 07:12
25

To get a black ProgressBar, use one of the inverse styles:

<ProgressBar style="@android:style/Widget.ProgressBar.Inverse"/>
<ProgressBar style="@android:style/Widget.ProgressBar.Large.Inverse"/>
<ProgressBar style="@android:style/Widget.ProgressBar.Small.Inverse"/>
Anm
  • 3,291
  • 2
  • 29
  • 40
10

The reason why the bar is of that color is because of the style you have selected. That is a fixed style and uses the system themes for the generation of UI elements.

Look at the source code from here. This progressBarStyleSmall attribute uses the styles defined here.

<style name="Widget.ProgressBar.Small">
    <item name="android:indeterminateDrawable">@android:drawable/progress_small_white</item>
    <item name="android:minWidth">16dip</item>
    <item name="android:maxWidth">16dip</item>
    <item name="android:minHeight">16dip</item>
    <item name="android:maxHeight">16dip</item>
</style>

Just create a custom style following the example from the android source code. You would need to replace the android:indeterminateDrawable to what you want.

Prashast
  • 5,645
  • 3
  • 30
  • 31
3
public static void setColorOfProgressBar(ProgressBar mProgressBar, int mColor){
    mProgressBar.getIndeterminateDrawable().setColorFilter(mColor, android.graphics.PorterDuff.Mode.MULTIPLY);
}
2

I had a very hard time trying to change it's color. To solve it, I got android's src code and did my own ProgressBar class.

Not the best answer, but it worked for me.

Macarse
  • 91,829
  • 44
  • 175
  • 230
2

And the answer was already on SO:

stackoverflow.com/questions/2020882/how-to-change-progress-bars-progress-color-in-android

You can simply change the whole drawable that is shown in the progressbar via the android:indeterminateDrawable attribute.

Community
  • 1
  • 1
Janusz
  • 187,060
  • 113
  • 301
  • 369
1

Just a note, to use

<ProgressBar style="@android:style/Widget.ProgressBar.Inverse"/> 

or any of the inverse progress bars, you have to set your minimum android sdk requirement to level 4 (Android 1.6).

ubzack
  • 1,878
  • 16
  • 16