0

I need to make a custom horizontal progressbar in Android, but it doesn't have to be continuous. It must have DIVIDERS for every progress value. For example: it must have green progress and white dividers. Please see the next image to understand what I'm looking for. Thank you.

enter image description here

For now I am trying to make a custom Drawable and set it to the progress bar.

public class CustomProgressDrawable extends Drawable {

    private static final int DEFAULT_RECT_WIDHT = 20;
    private static int DEFAULT_RECT_COLOR;

    private int rectWidth;
    private int rectColor;

    Paint paint = new Paint();

    public CustomProgressDrawable(Context context){
        init(context);
    }

    private void init(Context context){
        DEFAULT_RECT_COLOR = context.getResources().getColor(android.R.color.black);
        rectColor = DEFAULT_RECT_COLOR;
        rectWidth = DEFAULT_RECT_WIDHT; // Default rect width
    }


    @Override
    public void draw(Canvas canvas) {

        float parentWidth = 300;
        float parentHeight = 40;
        float childTop = 0;
        float childBottom = parentHeight;


        for (int i = rectWidth; i < parentWidth; i  =+ rectWidth){
            paint.setColor(rectColor);

            canvas.drawRect(i - rectWidth + 10, childTop, i + 10, childBottom, paint);

        }   
    }

    @Override
    protected boolean onLevelChange(int level) {

        invalidateSelf();

        return super.onLevelChange(level);

    } 
}

And in my may activity I have:

CustomProgressDrawable customProgressDrawable = new CustomProgressDrawable(this);

ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setProgressDrawable(customProgressDrawable);
progressBar.setProgress(50);
stanete
  • 4,062
  • 9
  • 21
  • 30

0 Answers0