Android: programmatically set color in ProgressBar

I would like to set the color programmatically to the progress indicator primaryProgress, secondaryProgress, since the color will change according to the background color on the screen.

code:

LayerDrawable progressDrawable = (LayerDrawable) ProgressBar1.getProgressDrawable(); Drawable backgroundColor = progressDrawable.getDrawable(0); Drawable secondaryColor = progressDrawable.getDrawable(1); Drawable primaryColor = progressDrawable.getDrawable(2); final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 }; primaryColor = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null)); secondaryColor = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null)); primaryColor.setColor((Color.rgb(color_normal[0], color_normal[1], color_normal[2])), null); secondaryColor.setColor((Color.rgb(color_normal[0], color_normal[1], color_normal[2])), null); progressDrawable.setDrawableByLayerId(progressDrawable.getId(2), new ClipDrawable(primaryColor, Gravity.LEFT, ClipDrawable.HORIZONTAL)); ... 

Edit:

** The color code here is for testing purposes only. Then the color code will refer to another part for updates, respectively.

  secondaryColor.setColorFilter((Color.rgb(255, 0, 0)), PorterDuff.Mode.SRC_OVER); primaryColor.setColorFilter((Color.rgb(0, 255, 213)), PorterDuff.Mode.SRC_OVER); progressDrawable.setDrawableByLayerId(progressDrawable.getId(2), new ClipDrawable(primaryColor, Gravity.LEFT, ClipDrawable.HORIZONTAL)); progressDrawable.setDrawableByLayerId(progressDrawable.getId(1), new ClipDrawable(secondaryColor, Gravity.LEFT, ClipDrawable.HORIZONTAL)); ProgressBar1.setProgressDrawable(progressDrawable); ProgressBar1.setProgress(progress); ProgressBar1.setSecondaryProgress(secondaryProgress); 

Question:

It underlines in red for primanyColor.setColor and reports that The method setColor(int, null) is undefined for the type Drawable .

How do I change the codes above to make them work? Thanks!

+5
source share
3 answers

To set colors for Drawable, use Drawable.setColorFilter . as:

 primaryColor.setColorFilter((Color.rgb(0,128,0)), PorterDuff.Mode.SRC_OVER); 
+2
source

What you can do is make your list of layers possible with xml first (this means that the background is used as the first layer, which is the secondary progress as the second layer, and the other is the main progress as the last layer), then change the color to the code by following these steps:

 public void setPrimaryProgressColor(int colorInstance) { if (progressBar.getProgressDrawable() instanceof LayerDrawable) { Log.d(mLogTag, "Drawable is a layer drawable"); LayerDrawable layered = (LayerDrawable) progressBar.getProgressDrawable(); Drawable circleDrawableExample = layered.getDrawable(<whichever is your index of android.R.id.progress>); circleDrawableExample.setColorFilter(colorInstance, PorterDuff.Mode.SRC_IN); progressBar.setProgressDrawable(layered); } else { Log.d(mLogTag, "Fallback in case it not a LayerDrawable"); progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN); } } 

This method will give you maximum flexibility in the fact that the measurement of the original original is declared in xml, WITHOUT MODIFICATION ON ITS STRUCTURE AFTER THE DEVICE, especially if you need to specify the folder with the xml screen file, and then just change ONLY COLOR through the code. There is no re-creation of a new ShapeDrawable from scratch.

+2
source

Here is the code to change the color of the ProgressBar programmatically,

 ProgressBar myProgress = (ProgressBar) findViewById(R.id.pb_listProgressBar); int colorCodeDark = Color.parseColor("#F44336"); myProgress.setIndeterminateTintList(ColorStateList.valueOf(colorCodeDark)); 
0
source

All Articles