Android ProgressDialog Example

Welcome to Android ProgressDialog Example. In this tutorial we’ll learn how to create Android Progress Dialog containing a ProgressBar. Also we’ll discuss at length the difference between a ProgressDialog and ProgressBar.

Android ProgressDialog

Android ProgressDialog is an extension of AlertDialog. To know more about an AlertDialog, check out it’s tutorial here.

Android ProgressDialog is a dialog box/dialog window which shows the progress of a task. Android Progress Dialog is almost same as ProgressBar with the exception that this is displayed as a dialog box.

In order to create a ProgressDialog to display a ProgressBar we need to instantiate it like this.

Difference between Android ProgressDialog and ProgressBar

  1. ProgressBar is a View (like TextView, ImageView, Button, etc..), which can be used in the layout to show some progress. A ProgressBar is used to indicate that something in tge app is still loading while the user may still interact with other parts
  2. ProgressDialog is a Dialog with ‘built-in’ ProgressBar. A ProgressDialog is used when we want to prevent the user from interacting with the application while waiting. The Dialog aspect freezes the user from doing anything until it is dismissed

Android ProgressDialog Attributes

Some important attributes of android ProgressDialog are given below.

  1. setMessage() : This method is used to show the message to the user. Example: Loading…
  2. setTitle() : This method is used to set a title to the dialog box
  3. setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) : This method is used to show the horizontal progress bar in the dialog box
  4. setProgressStyle(ProgressDialog.STYLE_SPINNER) : This method is used to show the circle/spinning progress bar in the dialog box
  5. setMax() : This method is used to set the maximum value
  6. getProgress() : This method is used to get the current progress value in numbers
  7. getMax() : This method returns the maximum value of the progress
  8. show(Context context, CharSequence title, CharSequence message) : This is a static method, used to display progress dialog
  9. incrementProgressBy(int diff) : This method increments the progress bar by the difference of value passed as a parameter

In this tutorial we’ll develop an application that displays a ProgressDialog containing a horizontal ProgressBar which increments after every 200 milliseconds.

Android ProgressDialog Project Structure

android-autocomplete-project-structure-236x450

Android ProgressDialog Example

The activity_main.xml contains a Button which invokes a ProgressDialog on click as shown in the xml code below:

activity_main.xml

The MainActivity.java file is given below.

MainActivity.java

The following code activates the handler in which we write the code to increment the progress bar.

Below is the output video when you will run the android progress dialog example application in android emulator.

android progressdialog example, android ProgressDialog, android progress dialog

This brings an end to Android ProgressDialog Example tutorial. You can download the final Android ProgressDialog Project from the below link.

By admin

Leave a Reply

%d bloggers like this: