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.
File : res/layout/main.xml
File : Main.java
i. The launch screen
ii. When 'Start Process' button is pressed.
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;@Overridepublic 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() {@Overridepublic void onClick(View v) {// prepare for a progress bar dialogprogressBar = 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 statusprogressBarStatus = 0;//reset processprocess = 0;new Thread(new Runnable() {public void run() {while (progressBarStatus < 100) {// process some tasksprogressBarStatus = doSomeTasks();// your computer is too fast, sleep for 1 secondtry {Thread.sleep(1000);}catch (InterruptedException e) {e.printStackTrace();}// Update the progress barprogressBarHandler.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 progresstry {Thread.sleep(2000);}catch (InterruptedException e) {e.printStackTrace();}// close the progress bar dialogprogressBar.dismiss();}}}).start();}});}// Process simulatorpublic 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