I would like to set color programmatically to the progress bar primaryProgress, secondaryProgress as the color will be changed according to the background color of 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 are for testing only. Afterwards the colorcode will be referenced to other part for updates accordingly
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 could I modify the above codes to make it works? Thanks!
primaryColor.getPaint().setColor(Color.rgb(color_normal[0], color_normal[1], color_normal[2]));
– AndroidThe method getPaint() is undefined for the type Drawable
– HideprimaryColor.setColorFilter
method – RianchoShapeDrawable primaryColor
then – AndroidsetColorFilter
as advised, with the rule that secondaryprogress must be greater than the primanyprogress, I dont know why it just showing the secondaryprogress (red) only. Pls see the updated code in the questions... thanks a lot! – Hide(0,128,0)
– Riancho