Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create circular progress bar(pie chart) like indicator - Android

Now I have a horizontal progress bar which is updated programmatically via ProgressBar setProgress method:

<ProgressBar
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="20dip"
            android:id="@+id/progressbar"
            >
    </ProgressBar>

Is there a way to convert this progress bar to a circle (pie chart) and to be able also to update the progress programmatically?

Example of what I want:

like image 541
Paul Avatar asked Sep 17 '12 11:09

Paul


People also ask

How do I make a custom circular progress bar?

This example demonstrates how do I make circle custom progress bar in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you make a custom circular progress bar in flutter?

Step 1: Create a radial gauge widget and add a radial axis into it. Step 2: Set the startAngle and endAngle as 270 to get a full circular scale and customize the axis look as required for progress bar. Step 3: Add the RangePointer and set the value to annotate the progress in the radial scale.


1 Answers

You can either make a custom view (e.g. PieProgressView) or a custom Drawable (e.g. PieProgressDrawable). I took the custom view approach but either is perfectly viable.

A quick look at the source for Android's ProgressView yields a wildly complex implementation. Obviously, they are covering all their bases, but you don't have to write something that complex. We really only need two things:

  1. A member to keep track of the current progress.
  2. A method to draw the pie based on the current progress.

Number one is easy, just keep a member field that tracks the current percentage of the pie to draw. Number 2 is a bit more complicated but, luckily, we can do it with the standard Canvas draw methods.

Conveniently, Android's Canvas class provides a drawArc() method. You can use this to get your Pie effect. Assuming we stored our percentage in a member field called mPercent as a float between 0 and 1, an onDraw() method might look like this:

@Override
protected void onDraw(Canvas canvas) {

    final float startAngle = 0f;
    final float drawTo = startAngle + (mPercent * 360);

    // Rotate the canvas around the center of the pie by 90 degrees
    // counter clockwise so the pie stars at 12 o'clock.
    canvas.rotate(-90f, mArea.centerX(), mArea.centerY());
    canvas.drawArc(mArea, startAngle, drawTo, true, mPaint);

    // Draw inner oval and text on top of the pie (or add any other
    // decorations such as a stroke) here..
    // Don't forget to rotate the canvas back if you plan to add text!
    ...
}

Here's what the completed view looks like in a sample app:

PieProgressView in action!

Edit

Since posting, I've decided there's really no reason you need to implement a custom view. You can simply use a drawable and it's level property to do exactly what is needed. I made a gist with the full drawable.

like image 69
dcow Avatar answered Oct 21 '22 00:10

dcow