Change background color with undefined horizontal pitch ProgressBar

I saw several topics regarding ProgressBars color processing, but no one could answer my doubts.

I am using the horizontal ProgressBar Undefined type. I want it to have a transparent background with a colored progressBar, but it cannot find a way to do this.

For a regular ProgressBar (not undefined), I can get LayerDrawable by calling progressBar.getProgressDrawable() . Then, using findDrawableByLayerId(android.R.id.background) , I can only paint the background.

But using progressBar.getIndeterminateDrawable() , it returns to me GradientDrawable , so I can not perform the same procedure.

Is it possible to get colors with GradientDrawable for all API levels? Because, if so, I could get the background color and change it.

Is there any solution for this?

+5
source share
3 answers

Use the color filter as follows:

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

progressBar.setProgressDrawable (R.drawable.progress_bar);

range hood / 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 the color in this file progress_bar.xml drawable

+1
source

Step # 1: Copy drawable/progress_indeterminate_horizontal.xml from your SDK resources to your project.

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

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

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

Step # 5: Change progress_indeterminate_horizontal.xml to specify your PNG files (as opposed to standard ones).

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

A source

Else

 progressBar.getIndeterminateDrawable() .setColorFilter(progressBar.getContext().getResources().getColor(<colorId>), PorterDuff.Mode.SRC_IN); 
0
source

Source: https://habr.com/ru/post/1263284/


All Articles