Tutorial

Android ProgressDialog Example

Published on August 4, 2022
author

Anupam Chugh

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.

ProgressDialog progress = new ProgressDialog(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 progress dialog, Android ProgressDialog, Android ProgressDialog Example

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

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Start ProgressDialog"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="57dp" />

</RelativeLayout>

The MainActivity.java file is given below. MainActivity.java

package com.journaldev.progressdialog;

import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button button;
    ProgressDialog progressDoalog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressDoalog = new ProgressDialog(MainActivity.this);
                progressDoalog.setMax(100);
                progressDoalog.setMessage("Its loading....");
                progressDoalog.setTitle("ProgressDialog bar example");
                progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDoalog.show();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            while (progressDoalog.getProgress() <= progressDoalog
                                    .getMax()) {
                                Thread.sleep(200);
                                handle.sendMessage(handle.obtainMessage());
                                if (progressDoalog.getProgress() == progressDoalog
                                        .getMax()) {
                                    progressDoalog.dismiss();
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }

            Handler handle = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    progressDoalog.incrementProgressBy(1);
                }
            };
        });
    }
}

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

handle.sendMessage(handle.obtainMessage());

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.

Download Android ProgressDialog Project

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Anupam Chugh

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
April 29, 2019

I want to implement object detection application. where it captures the images and detects objects in it. I tried so many demo’s but cant get the accuracy. help me if you find anything useful on it. Thanks in advance.

- Mayur Kodhe

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 10, 2019

    thank you very much for the code

    - chamira

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      May 4, 2017

      In your code, there is a miss-typo " if (progressDoalog.getProgress() " -> " if (progressDialog.getProgress() "

      - Tao

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        October 4, 2016

        Thank you so much

        - saran

          Try DigitalOcean for free

          Click below to sign up and get $200 of credit to try our products over 60 days!

          Sign up

          Join the Tech Talk
          Success! Thank you! Please check your email for further details.

          Please complete your information!

          Become a contributor for community

          Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

          DigitalOcean Documentation

          Full documentation for every DigitalOcean product.

          Resources for startups and SMBs

          The Wave has everything you need to know about building a business, from raising funding to marketing your product.

          Get our newsletter

          Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

          New accounts only. By submitting your email you agree to our Privacy Policy

          The developer cloud

          Scale up as you grow — whether you're running one virtual machine or ten thousand.

          Get started for free

          Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

          *This promotional offer applies to new accounts only.