Friday, February 3, 2017

Changing background color of Indeterminate Horizontal ProgressBar

Leave a Comment

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?

3 Answers

Answers 1

Use color filter like this:

progressBar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.MULTIPLY); 

Answers 2

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); 

Answers 3

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

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment