0

I want to change the appearance on my progress bar dynamically using the setProgressDrawable method, and without involving xml files.

I saw this answer : Create a progress drawable programmatically ,but some reason, it sets only the progress background color. The actual progress color does not even appear. Is what I'm doing right?

My code:

public static LayerDrawable createDrawable(int accentColor, int backColor) {

    ShapeDrawable shape = new ShapeDrawable();
    shape.getPaint().setStyle(Style.FILL);
    shape.getPaint().setColor(backColor);

    ShapeDrawable shapeD = new ShapeDrawable();
    shapeD.getPaint().setStyle(Style.FILL);
    shapeD.getPaint().setColor(accentColor);
    ClipDrawable clipDrawable = new ClipDrawable(shapeD, Gravity.LEFT,
            ClipDrawable.HORIZONTAL);

    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] {
             clipDrawable });
    return layerDrawable;
}

Setting a new drawable

progressBar[i].setProgress(Integer.parseInt(internal_marks[i]));
progressBar[i].setProgressDrawable(createDrawable(accent_list[mIndex],getResources().getColor(R.color.progressBack)));

FYI: It works fine if I use a xml instead of creating a drawable synamically, but I want to do it dynamically.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Shubham Kanodia
  • 6,036
  • 3
  • 32
  • 46

0 Answers0