How To Make Circle Custom Progress Bar in Android?



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.

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:padding="8dp"    tools:context=".MainActivity"> <ProgressBar    android:id="@+id/circularProgressbar"    style="?android:attr/progressBarStyleHorizontal"    android:layout_width="250dp"    android:layout_height="250dp"    android:layout_centerInParent="true"    android:indeterminate="false"    android:max="100"    android:progress="50"    android:secondaryProgress="100" /> <TextView    android:id="@+id/textView"    android:layout_width="250dp"    android:layout_height="250dp"    android:gravity="center"    android:text="25%"    android:layout_centerInParent="true"    android:textColor="@color/colorPrimaryDark"    android:textSize="24sp" /> </RelativeLayout>

Step 3 − Create a drawable resource file(circularprogressbar.xml) and add the following code −

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content">    <item android:id="@android:id/secondaryProgress">       <shape          android:innerRadiusRatio="6"          android:shape="ring"          android:thicknessRatio="20.0"          android:useLevel="true">          <gradient             android:centerColor="#999999"             android:endColor="#999999"             android:startColor="#999999"             android:type="sweep" />       </shape>    </item>    <item android:id="@android:id/progress">       <rotate          android:fromDegrees="270"          android:pivotX="50%"          android:pivotY="50%"          android:toDegrees="270">          <shape             android:innerRadiusRatio="6"             android:shape="ring"             android:thicknessRatio="20.0"             android:useLevel="true">             <gradient                android:centerColor="#00FF00"                android:endColor="#00FF00"                android:startColor="#00FF00"                android:type="sweep" />                <rotate                   android:fromDegrees="0"                   android:pivotX="50%"                   android:pivotY="50%"                   android:toDegrees="360" />          </shape>       </rotate>    </item>    <item android:id="@android:id/secondaryProgress">       <shape          android:innerRadiusRatio="6"          android:shape="ring"          android:thicknessRatio="20.0"          android:useLevel="true">          <gradient             android:centerColor="#999999"             android:endColor="#999999"             android:startColor="#999999"             android:type="sweep" />       </shape>    </item> </layer-list>

Step 4 − Add the following code to src/MainActivity.java

import androidx.appcompat.app.AppCompatActivity; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Handler; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends AppCompatActivity {    int status = 0;    private Handler handler = new Handler();    TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       Resources resources = getResources();       Drawable drawable = resources.getDrawable(R.drawable.circularprogressbar);       final ProgressBar progressBar = findViewById(R.id.circularProgressbar);       progressBar.setProgress(0);       progressBar.setSecondaryProgress(100);       progressBar.setMax(100);       progressBar.setProgressDrawable(drawable);       textView = findViewById(R.id.textView);       new Thread(new Runnable() {          @Override          public void run() {             while (status < 100) {                status += 1;                handler.post(new Runnable() {                   @Override                   public void run() {                      progressBar.setProgress(status);                      textView.setText(String.format("%d%%", status));                   }                });                try {                   Thread.sleep(16);                }                catch (InterruptedException e) {                   e.printStackTrace();                }             }          }       }).start();    } }

Step 5 − Add the following code to androidManifest.xml

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from the android studio, open one of your project's activity files and click the RunPlay Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Updated on: 2020-07-07T13:47:56+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements