Introduction
This article explains how to use a Progress Bar.
A Progress Bar is a graphical user interface that shows the progress of a task. Initially, the Progress Bar does not show the progress in the form of a percentage. But now a Progress Bar shows progress in the form of a percentage. There are two types of Progress Bars.
ProgressDialogue Style Spinner
Progress Bar
ProgressDialogue Style Horizontal
ProgressDialogue Style_Spinner
ProgressDialogue Style_Horizontal
In this, you will use a button to start the progress bar in a click event. Inside the click event you will write this:


- progress = new ProgressDialog(v.getContext());
- progress.setMessage("Show Progress ...");
- progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- progress.setProgress(0);
- progress.setMax(100);
- progress.show();
- progressStatus = 0;
- fileSize = 0;
- new Thread(new Runnable()
- {
- public void run() {
- while (progressStatus < 100) {
- progressStatus = doSomeTasks();
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- progressBarHandler.post(new Runnable() {
- public void run() {
- progress.setProgress(progressStatus);
- }
- });
- }
- if (progressStatus >= 100) {
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- progress.dismiss();
- }
- }
- }).start();
Step 1
Create an XML file and write this:
- <?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:orientation="vertical" >
- <Button
- android:id="@+id/btnStartProgress"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="StartProgress" />
- </LinearLayout>
Step 2
Create a Java file and write this:
- package com.progress;
- 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 MainActivity extends Activity {
- Button btnshowprogress;
- ProgressDialog progress;
- private int progressStatus = 0;
- private Handler progressBarHandler = new Handler();
- private long fileSize = 0;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btnshowprogress = (Button) findViewById(R.id.btnStartProgress);
- btnshowprogress.setOnClickListener(
- new OnClickListener() {
- @Override
- public void onClick(View v) {
- // prepare for a progress bar dialog
- progress = new ProgressDialog(v.getContext());
- progress.setMessage("Show Progress ...");
- progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- progress.setProgress(0);
- progress.setMax(100);
- progress.show();
- //reset progress bar status
- progressStatus = 0;
- //reset filesize
- fileSize = 0;
- new Thread(new Runnable() {
- public void run() {
- while (progressStatus < 100) {
- // process some tasks
- progressStatus = doSomeTasks();
- // your computer is too fast, sleep 1 second
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- // Update the progress bar
- progressBarHandler.post(new Runnable() {
- public void run() {
- progress.setProgress(progressStatus);
- }
- });
- }
- // ok, file is downloaded,
- if (progressStatus >= 100) {
- // sleep 2 seconds, so that you can see the 100%
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- // close the progress bar dialog
- progress.dismiss();
- }
- }
- }).start();
- }
- });
- }
- // file download simulator... a really simple
- public int doSomeTasks() {
- while (fileSize <= 1000000) {
- fileSize++;
- if (fileSize == 100000) {
- return 10;
- } else if (fileSize == 200000) {
- return 20;
- } else if (fileSize == 300000) {
- return 30;
- }
- // ...add your own
- }
- return 100;
- }
- }
Step 3


Comments
Join the conversation! Your thoughts help the community grow.