Question

I am looking for a way to make a progress bar fill effect on Android. I have a FrameLayout called progressBar and inside this FrameLayout is a LinearLayout called filledBar. I don't know the literal widths of either progressBar or filledBar, but I would like filledBar to take up a certain amount of progressBar. How would I do this? (I am creating both bars programatically)

Was it helpful?

Solution

quite simple, do a XML layout like this:

FrameLayout
    View // background set to be a custom drawable
    TextView // gravity right

in code you inflate it and set the drawable:

private CustomDrawable drawable;

drawable = new CustomDrawable(context);
View v = layout.findViewById(...
v.setBackground(drawable);

then to update the percentage

drawable.setLevel(10);  // for 10% for example

when you create your custom drawble inside the draw method it's a simple call to drawRect

{

canvas.drawRect(0, 0, getLevel()/100*canvas.getWidth(), canvas.getHeight, paint);

}

the paint you create with the color you want it to be.

That's a very rough idea, but I hope it's enough to guide you to the correct direction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top