Android program to implement ProgressBar

Android program to implement ProgressBar

Android ProgressBar is a graphical view which shows progress, that is how much of the task is being completed. For example, while downloading a file from the internet, it shows how much is completed, while updating your android app, it shows how much percent of the app has been updated.

ProgressBar can be of two types Horizontal or Circular.

Attributes associated with ProgressBar are:-

  1. android:max : To set the maximum value of the ProgressBar using this attribute. By default, the maximum value of the progressbar is 100.
  2. android:determinate : Set it is as true, which shows that loading amount is measured, like in terms of percantage. We generally set the progressStyle of the progressDialog to Horizontal
  3. android:indeterminate : Set it is as true, which shows that loading amount is not measured. We generally set the progressStyle of the progressDialog to Circular

Methods associated with ProgressDialog are:-

  1. setProgress(int value): This method is used to set the current progress to a specific value.
  2. getMax(): This method returns the maximum value of the progress.
  3. setIndeterminate(boolean indeterminate): This method sets the progress indicator as determinate or indeterminate.

In this program, we will show the progressDialog when we click on the dialog button. We will increment the progress status by 1 and set the maximum to 100. We keep incrementing till we reach 100, and once we reach 100 we will clear the progressDialog using dismiss() method.

Steps to create the application:- 

  • Open Android Studio and create a new Android application and name it as “ProgressBar” and company domain as codedost so your package will be automatically set.
  • Open an Empty Activity and name it as MainActivity.
  • Copy the contents of res/layout/activity_main.xml file.
  • Run the application to launch Android emulator or you can run it on your mobile also(which is way faster).

XML File(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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="codedost.progressbar.MainActivity">

    <Button
        android:text="Download"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/button" />
</RelativeLayout>

MainActivity.java

package com.example.jay.progressbar_clg;

import android.app.ProgressDialog;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ProgressDialog pb;
    int progressStatus=0;
    TextView tv;
    Button b1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       final Handler handler=new Handler();

        b1=(Button) findViewById(R.id.button);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pb=new ProgressDialog(v.getContext());
                pb.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pb.setCancelable(true);
                pb.setMessage("Downloading");
                pb.setProgress(0);
                pb.setMax(100);
                pb.show();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (progressStatus <100) {
                            progressStatus += 1;
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    pb.setProgress(progressStatus);
                                }

                            });

                            try {
                                Thread.sleep(50);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        if(progressStatus>=100)
                        {
                            try{
                                Thread.sleep(1000);
                            }
                            catch (Exception e) {
                                e.printStackTrace();
                            }
                            pb.dismiss();
                            progressStatus=0;
                        }
                    }
                }).start();
            }
        });
    }
}

Output

Share Me!