6

I've seen some topics about handling colors of ProgressBars but none could answer my doubt.

I am using a horizontal ProgressBar Indeterminate type. I want it to have a transparent background while having a colored progressBar but cannot find a way to do it.

For a normal ProgressBar (not Indeterminate) I can get a LayerDrawable by calling progressBar.getProgressDrawable(). Then using findDrawableByLayerId(android.R.id.background), I am able to tint just the background.

But using progressBar.getIndeterminateDrawable() it returns me a GradientDrawable, so I can't follow the same procedure.

Is it possible to get the colors from a GradientDrawable for all APIs levels? Cause if it is, I could get the color of the background and change it.

Is there any solution for this?

AndroidDev
  • 831
  • 5
  • 17
  • 36

3 Answers3

2

Use color filter like this:

progressBar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
Saman Salehi
  • 1,995
  • 1
  • 22
  • 36
1

progressBar.setProgressDrawable(R.drawable.progress_bar);

drawable/progress_bar.xml

<layer-list   xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape>
        <corners android:radius="2dip" />
        <gradient android:startColor="#43ffffff" android:centerColor="#43ffffff" android:centerY="0.75" android:endColor="#43ffffff" android:angle="270" />
    </shape>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners android:radius="2dip" />
            <gradient android:startColor="#fff" android:endColor="#fff" android:angle="270" />
        </shape>
    </clip>
</item> </layer-list>

you can change color in this progress_bar.xml drawable

Baqar Gogia
  • 367
  • 4
  • 17
0

Step #1: Copy drawable/progress_indeterminate_horizontal.xml from your SDK's resources into your project.

Step #2: Copy drawable-hdpi/progressbar_indeterminate* from your SDK's resources into your project.

Step #3: Copy drawable-mdpi/progressbar_indeterminate* from your SDK's resources into your project.

Step #4: Modify the PNG files to look the way you want.

Step #5: Modify the progress_indeterminate_horizontal.xml to point to your PNG files (versus the standard ones).

Step #6: Use @drawable/progress_indeterminate_horizontal for the indeterminateDrawable value.

Source

Else

progressBar.getIndeterminateDrawable()
    .setColorFilter(progressBar.getContext().getResources().getColor(<colorId>),
        PorterDuff.Mode.SRC_IN);
Community
  • 1
  • 1
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • This will change all Indeterminate progress bars in my project, right? If so, is there any workaround to apply it for a single progressbar in my project? – AndroidDev Jan 31 '17 at 12:14
  • @AndroidDev, (1st.) for all and (2nd.)nope, for the particular one – W4R10CK Jan 31 '17 at 12:17