1

I wanted to show profile status using a Progress bar just like Linkedin, I searched everywhere but didn't get any reference related to that. enter image description here

My code:

 public class ProgressDrawable extends Drawable {
private static final int NUM_SEGMENTS = 5;
private final int mForeground;
private final int mBackground;
private final Paint mPaint = new Paint();
private final RectF mSegment = new RectF();

public ProgressDrawable(int fgColor, int bgColor) {
    mForeground = fgColor;
    mBackground = bgColor;
}

@Override
protected boolean onLevelChange(int level) {
    invalidateSelf();
    return true;
}

@Override
public void draw(Canvas canvas) {
    float level = getLevel() / 10000f;
    Rect b = getBounds();
    float gapWidth = b.height() / 10f;
    float segmentWidth = (b.width() - (NUM_SEGMENTS - 1) * gapWidth) / NUM_SEGMENTS;
    mSegment.set(0, 0, segmentWidth, b.height());
    mPaint.setColor(mForeground);

    for (int i = 0; i < NUM_SEGMENTS; i++) {
        float loLevel = i / (float) NUM_SEGMENTS;
        float hiLevel = (i + 1) / (float) NUM_SEGMENTS;
        if (loLevel <= level && level <= hiLevel) {
            float middle = mSegment.left + NUM_SEGMENTS * segmentWidth * (level - loLevel);
            canvas.drawRect(mSegment.left, mSegment.top, middle, mSegment.bottom, mPaint);
            mPaint.setColor(mBackground);
            canvas.drawRect(middle, mSegment.top, mSegment.right, mSegment.bottom, mPaint);
        } else {
            canvas.drawRect(mSegment, mPaint);
        }
        mSegment.offset(mSegment.width() + gapWidth, 0);
    }
}

@Override
public void setAlpha(int alpha) {
}

@Override
public void setColorFilter(ColorFilter cf) {
}

@Override
public int getOpacity() {
    return PixelFormat.TRANSLUCENT;
}

}

In this code, all the blocks have same color, but as per requirement, all block colors should be different.

  • 2
    Have you check this : https://stackoverflow.com/questions/14581602/android-how-to-implement-horizontal-step-progress-bar – AskNilesh Dec 09 '19 at 06:21
  • you can also check this : https://stackoverflow.com/questions/44989575/how-to-create-step-wise-progress-bar-in-android – Evyatar Cohen Dec 18 '19 at 07:25

0 Answers0