Wednesday, August 6, 2014

Use Progress Bar in Android

Progress bar is a way to convey to the user that the task will take time to complete. It may also be used to show the progress of the activity, and hence the name.
In this tutorial, we show you how to display a progress bar dialog to tell user that your task is running, and also how to increase the progress bar status until the task is completed.

1. Add a button

Open the main.xml file and add a button for demonstration sake.
File : res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >
    <Button android:id="@+id/btnStartProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Process" />
</LinearLayout>

2. Java Code

For using the Progress Bar, use “Thread” to run your time consuming task and another “Thread” to keep the progress bar status updating accordingly. The code and comments shall be self-explanatory.
File : Main.java

package com.endeavour.tutorial;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class Main extends Activity {
       Button btnStartProgress;
       ProgressDialog progressBar;
       private int progressBarStatus = 0;
       private Handler progressBarHandler = new Handler();
       private long process = 0;
       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              addListenerOnButton();
       }
       public void addListenerOnButton() {
              btnStartProgress = (Button) findViewById(R.id.btnStartProgress);
              btnStartProgress.setOnClickListener(new OnClickListener() {
                     @Override
                     public void onClick(View v) {
                           // prepare for a progress bar dialog
                           progressBar = new ProgressDialog(v.getContext());
                           progressBar.setCancelable(true);
                           progressBar.setMessage("Processing...");
                           progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                           progressBar.setProgress(0);
                           progressBar.setMax(100);
                           progressBar.show();
                           //reset progress bar status
                           progressBarStatus = 0;
                           //reset process
                           process = 0;
                            new Thread(new Runnable() {
                               public void run() {
                                   while (progressBarStatus < 100) {
                                      // process some tasks
                                      progressBarStatus = doSomeTasks();
                                     // your computer is too fast, sleep for 1 second
                                               try {
                                                       Thread.sleep(1000);
                                                }
 catch (InterruptedException e) {
                                                       e.printStackTrace();
                                                }
                                    // Update the progress bar
                                    progressBarHandler.post(new Runnable() {
                                        public void run() {
                                           progressBar.setProgress(progressBarStatus);
                                                       }
                                                });
                                         }
                                         // process is completed,
                                         if (progressBarStatus >= 100) {
                        // sleep 2 seconds, so that you can see the 100% activity progress
                                                try {
                                                       Thread.sleep(2000);
                                                }
catch (InterruptedException e) {
                                                       e.printStackTrace();
                                                }
                                                // close the progress bar dialog
                                                progressBar.dismiss();
                                         }
                                  }
                           }).start();
                     }
              });
       }
       // Process simulator
       public int doSomeTasks() {
              while (process <= 1000000) {
                     process++;
                     if (process == 100000) {
                           return 10;
                     } else if (process == 200000) {
                           return 20;
                     } else if (process == 300000) {
                           return 30;
                     }
                     else if (process == 400000) {
                           return 40;
                     }
                     else if (process == 500000) {
                           return 50;
                     }
                     // and so on...
              }
              return 100;
       }
}
Note:
We can use 'STYLE_SPINNER' attribute in place of 'STYLE_HORIZONTAL' in progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); attribute to show a progress wheel instead of a progress bar.

3. Output

The following output shall be displayed.

i. The launch screen


ii. When 'Start Process' button is pressed.

No comments:

Post a Comment