Android ProgressBar Example

Progress-bar is used to show the progress of an operation.

Step : 1 — main.xml

<LinearLayout
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

   <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:gravity="center_horizontal"
        android:textSize="25dp"
        android:text="Progress Bar Example" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:onClick="startProgressDialog"
        android:text="Start Downloading File" />

</LinearLayout>

Step : 2 — ProgressBarActivity.java

public class ProgressBarActivity extends Activity
{
   

    Button btnStartProgress;
    ProgressDialog progressBar;
    private int progressBarStatus = 0;
    private Handler progressBarHandler = new Handler();

    private long fileSize = 0;

    /** Called when the activity is first created. */ 
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    }

    public void startProgressDialog(View V) 
    {
        

                    // prepare for a progress bar dialog
                    progressBar = new ProgressDialog(V.getContext());
                    progressBar.setCancelable(true);
                    progressBar.setMessage("Downloading File...");
                    progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    progressBar.setProgress(0);
                    progressBar.setMax(100);
                    progressBar.show();

                    //reset progress bar status
                    progressBarStatus = 0;

                    //reset filesize
                    fileSize = 0;

                    new Thread(new Runnable() {
                        public void run() {
                            while (progressBarStatus < 100) {

                                    // process some tasks
                                progressBarStatus = fileDownloadStatus();

                                //  sleep 1 second to show the progress
                                try {
                                        Thread.sleep(1000);
                                    } 
                                catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }

                                // Update the progress bar
                                progressBarHandler.post(new Runnable() {
                                    public void run() {
                                        progressBar.setProgress(progressBarStatus);
                                    }
                                });
                            }

                            // when, file is downloaded 100%,
                            if (progressBarStatus >= 100) {

                                // sleep 2 seconds, so that you can see the 100% of file download
                                try {
                                    Thread.sleep(2000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }

                                // close the progress bar dialog
                                progressBar.dismiss();
                            }
                        }
                    }).start();

      }


       


  //method returns the % of file downloaded
    public int fileDownloadStatus()
    {

        while (fileSize <= 1000000) {

                      fileSize++;

                       if (fileSize == 100000) {
                            return 10;
                       } else if (fileSize == 200000) {
                           return 20;
                       } else if (fileSize == 300000) {
                          return 30;
                       } else if (fileSize == 400000) {
                          return 40;
                       } else if (fileSize == 500000) {
                          return 50;
                      } else if (fileSize == 600000) {
                         return 60;
                      }
            // write your code here

        }

        return 100;

    }

Enjoy Code.