Introduction
This article explains how to use a Progress Bar.
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
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:
Step 4
When you will click on a button: