16

I am currently using an external library in my Android project imported via gradle.
This library show a notification bar with a ProgressBar circle. This is the code I found in it's sources :

<ProgressBar
            android:id="@+id/progress_bar"
            android:layout_height="match_parent"
            android:layout_marginBottom="4dp"
            android:layout_marginTop="4dp"
            style="@style/SuperActivityToast_Progress_ProgressBar"/>

The style associated is this one :

<style name="SuperActivityToast_Progress_ProgressBar" parent="android:Widget.Holo.ProgressBar">
    <item name="android:layout_width">32dp</item>
    <item name="android:layout_marginLeft">8dp</item>
</style>

If I understand correclty, the color of the circle shown is derived from the default one ( green on my phone ). I need to change it!

Now, I can't modify the source code, and the library itself doesn't offer me the possibility to set the style programmatically.

There is a way to change the default style at app level or better override this specific style?

Thanks Davide

Nickolaus
  • 4,785
  • 4
  • 38
  • 60
Davide
  • 583
  • 1
  • 6
  • 9
  • possible duplicate of [Android change Horizonal Progress bar color](http://stackoverflow.com/questions/5745814/android-change-horizonal-progress-bar-color) – wbk727 Aug 17 '15 at 18:02

4 Answers4

62

If you are using the AppCompat theme, it uses the accentColor to tint the circle.

If you want to tint it to a different color than the Theme, then you should consider using ThemeOverylay. E.g. If you want to make the circle tint red you could do the following:

in your styles.xml

<style name="RedAccent" parent="ThemeOverlay.AppCompat.Light">
    <item name="colorAccent">#F00</item>
</style>

in your ProgressBar, set the theme to be RedAccent.

<ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:theme="@style/RedAccent"/>

And your circle will now be in red color!

bond
  • 11,236
  • 7
  • 48
  • 62
30

After several attempts I found a solution :

ProgressBar progBar = (ProgressBar) context.getActivity().findViewById(R.id.progress_bar);
if (progBar != null) {
    progBar.setVisibility(View.VISIBLE);
    progBar.setIndeterminate(true);
    progBar.getIndeterminateDrawable().setColorFilter(0xFFFFFFFF, android.graphics.PorterDuff.Mode.MULTIPLY);
}

Simply, i'll get a reference of the progress bar object created by the library and i change it's attributes. ( in my activity i must do that in a "OnStart" method otherwise it is null ) The most important part is the "setColorFilter" that do the magic.

Davide
  • 583
  • 1
  • 6
  • 9
  • 1
    Mode.MULTIPLY will multiply the color with progressbar color, if someone wants pure solid color then use PorterDuff.Mode.SRC_ATOP – Ramesh Kumar Apr 20 '16 at 11:09
12

For future references, this change has worked for me is:

Changing the colorControlActivated within AppTheme in your values/styles.xml file:

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Main theme colors -->
    ....
    <!-- Color for circle in progress bar -->
    <item name="colorControlActivated">#DC0808</item>
</style>

With this approach, you do not need to perform any action on your <ProgressBar/> tag within your xml file.

Red M
  • 2,609
  • 3
  • 30
  • 50
8

Just add color in ProgressBar like below :

    <ProgressBar
    android:id="@+id/progressbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:visibility="gone"
    android:indeterminateTint="@color/colorPrimary"  // add color here
    android:layout_centerVertical="true"/>
murugan mani
  • 375
  • 5
  • 6