13 Program to display circular progress bar in Android Studio

AndroidManifest.xml:


<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.blogspot.codingatharva.manualprograms">



<application

    android:allowBackup="true"

    android:icon="@mipmap/ic_launcher"

    android:label="@string/app_name"

    android:roundIcon="@mipmap/ic_launcher_round"

    android:supportsRtl="true"

    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">

        <intent-filter>

            <action android:name="android.intent.action.MAIN" />



            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

    </activity>

</application>



</manifest>


MainActivity.java:


package com.blogspot.codingatharva.manualprograms;



import androidx.appcompat.app.AppCompatActivity;



import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ProgressBar;



public class MainActivity extends AppCompatActivity {



    Button b;

    ProgressBar pb;

    private int progressStatus = 0;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        b = findViewById(R.id.btn);

        pb = findViewById(R.id.progressBar);



        b.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                startProgress(v);

            }

        });

    }



    public void startProgress(View view) {

        pb.setProgress(0);

        new Thread(new Task()).start();

    }



    class Task implements Runnable {

        @Override

        public void run() {

            for (int i = 0; i <= 10; i++) {

                final int value = i;

                try {

                    Thread.sleep(1000);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

                pb.setProgress(value);



            }

        }



    }

}


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:layout_margin= "16dp" tools:context= ".MainActivity" > <ProgressBar android:id= "@+id/progressBar" style= "?android:attr/progressBarStyleHorizontal" android:layout_width= "200dp" android:layout_height= "200dp" android:layout_centerInParent= "true" android:background= "@drawable/circular_shape" android:indeterminate= "false" android:max= "100" android:progress= "0" android:progressDrawable= "@drawable/circular_progress_bar" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_below="@+id/progressBar" android:text="Start"/> </RelativeLayout>

circular_progress_bar.xml: under res/drawable

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android = "http://schemas.android.com/apk/res/android"
    android:fromDegrees= "270"
    android:toDegrees= "270" >
    <shape
        android:innerRadiusRatio= "2.5"
        android:shape= "ring"
        android:thickness= "1dp"
        android:useLevel= "true" > <!-- this line fixes the issue for lollipop api 21 -->
        <gradient
            android:angle= "0"
            android:endColor= "#007DD6"
            android:startColor= "#007DD6"
            android:type= "sweep"
            android:useLevel= "false" />
    </shape>
</rotate>

circular_shape.xml: under res/drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android"
    android:innerRadiusRatio= "2.5"
    android:shape= "ring"
    android:thickness= "1dp"
    android:useLevel= "false" >
    <solid android:color= "#CCC" />
</shape>

Output:

Program to display circular progress bar in Android Studio

Previous
Next Post »