Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing background color of Indeterminate Horizontal ProgressBar

Tags:

android

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?

like image 377
AndroidDev Avatar asked Oct 17 '22 18:10

AndroidDev


2 Answers

Use color filter like this:

progressBar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
like image 161
Saman Salehi Avatar answered Oct 30 '22 03:10

Saman Salehi


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

like image 30
Baqar Gogia Avatar answered Oct 30 '22 03:10

Baqar Gogia