Learn Android UI

ProgressDialog Tutorial With Example In Android Studio

Android Progress Dialog is a UI which shows the progress of a task like you want user to wait until the previous lined up task is completed and for that purpose you can use progress dialog. The best example is you see when downloading or uploading any file.

ProgressDialog Example In Android Studio

In this tutorial I will demonstrate both the Ring and the Bar style(Horizontal) ProgressDialog component and will also use the special tools like Handler to update the value of the bar format.

  ProgressDialog progressDialog = new ProgressDialog(this);

Important Methods Of ProgressDialog

Progress Dialog Methods In Android Studio
1. setTitle(CharSequence title) – This component is used to set the title of the progress dialog.

// Setting Title
progressDialog.setTitle("ProgressDialog"); 

2. setMessage(CharSequence message) – This component displays the required message in the progress dialog.

// Setting Message
progressDialog.setMessage("Loading..."); 

3. setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) – This is used for setting the horizontal style of the progress dialog.

// Progress Dialog Style Horizontal
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

4. setProgressStyle(ProgressDialog.STYLE_SPINNER) – This is used for setting the spinner style of the progress dialog.

// Progress Dialog Style Spinner
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 

Progress Dialog Style Spinner In Android Studio
5. setMax(int max) – This method sets the maximum value of the progress dialog.

// Progress Dialog Max Value
progressDialog.setMax(100);

6. getMax() – This method return the maximum value of the progress dialog, basically this method is used while applying condition over the progress dialog.

// Fetching max value
progressDialog.getMax();

7. getProgess() – This returns current progress of the progress dialog in numeric.

// Fetching current progress
progressDialog.getProgress();

8. incrementProgressBy(int diff) – This method increments the progress dialog value with the defined value.

 // Incremented By Value 2
progressDialog.incrementProgressBy(2); 

9. setCancelable(boolean cancelable) – This method has boolean value i.e true/false. If set to false it allows to cancel the dialog box by clicking on area outside the dialog default it is true if method is not used.

// Cannot Cancel Progress Dialog
progressDialog.setCancelable(false); 

10. dismiss() – This method dismiss the progressdialog.

//Dismiss the dialog
progressDialog.dismiss();

ProgressDialog Example In Android Studio:

Below is the example of Progress Dialog in which the functionality of ProgressDialog is defined over the buttons click. In this example we have used a simple buttons and over that button click the ProgressDialog will appear.

Below you can download code, see final output and step by step explanation of ProgressDialog example in Android Studio.

Download Code

ProgressDialog Example In Android Studio
Step 1: Create a new project and name it ProgressDialogExample.

Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code:

Here define two buttons

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.progessdialogexample.MainActivity"
    android:elevation="1dp">

    <Button
        android:text="  Click To View Ring Progress Dialog..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="171dp"
        android:id="@+id/button"
        style="@android:style/Widget.DeviceDefault.Button.Inset"
        android:textStyle="normal|bold"
        android:textSize="15sp"
        android:textColor="@android:color/background_dark"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:text="  Click To View Progress Dialog..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@android:style/Widget.DeviceDefault.Button.Inset"
        android:layout_marginTop="49dp"
        android:textSize="15sp"
        android:id="@+id/button2"
        android:textStyle="normal|bold"
        android:textColor="@android:color/background_dark"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Step 3 : Now open app -> java -> package -> MainActivity.java and add the below code.

In this we added the progress dialog functionality on the button click having onclicklistener.  The two buttons both are displaying different type of progress dialog which is set using setProgressStyle i.e spinner or horizontal bar.

package com.example.progessdialogexample;

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.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button b1, b2;
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.button);
        b2 = (Button) findViewById(R.id.button2);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMessage("Loading..."); // Setting Message
                progressDialog.setTitle("ProgressDialog"); // Setting Title
                progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // Progress Dialog Style Spinner
                progressDialog.show(); // Display Progress Dialog
                progressDialog.setCancelable(false);
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            Thread.sleep(10000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        progressDialog.dismiss();
                    }
                }).start();
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {
            Handler handle = new Handler() {
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    progressDialog.incrementProgressBy(2); // Incremented By Value 2
                }
            };

            @Override
            public void onClick(View v) {
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMax(100); // Progress Dialog Max Value
                progressDialog.setMessage("Loading..."); // Setting Message
                progressDialog.setTitle("ProgressDialog"); // Setting Title
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // Progress Dialog Style Horizontal
                progressDialog.show(); // Display Progress Dialog
                progressDialog.setCancelable(false);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            while (progressDialog.getProgress() <= progressDialog.getMax()) {
                                Thread.sleep(200);
                                handle.sendMessage(handle.obtainMessage());
                                if (progressDialog.getProgress() == progressDialog.getMax()) {
                                    progressDialog.dismiss();
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }
}

Output:

Now start the AVD in Emulator and run the App. You will see two button. Click on them and see the different ProgressDialog in android.

DOWNLOAD THIS FREE eBook!

This free eBook will help you master the learning of Android App Development in Android Studio!

3 thoughts on “ProgressDialog Tutorial With Example In Android Studio”

  1. Handler handle = new Handler() {
    public void handleMessage() {
    super.handleMessage(android.os.Message.obtain());
    dialog.incrementProgressBy(2); // Incremented By Value 2
    }
    };
    handleMessage() may not be called

Leave a Reply

Your email address will not be published. Required fields are marked *



Continue Reading Below Tutorial

Download Free - Master Android App Development Sidebar

DOWNLOAD THIS FREE eBook!

This free eBook will help you master the learning of Android App Development in Android Studio!
close-link

Android Developer Facebook Group Free

Premium Project Source Code:




DOWNLOAD THIS FREE eBook!

This free eBook will help you master the learning of Android App Development in Android Studio!
close-link

DOWNLOAD THIS FREE eBook!

This free eBook will help you master the learning of Android App Development in Android Studio!
close-link

DOWNLOAD THIS FREE eBook!

This free eBook will help you master the learning of Android App Development in Android Studio!
close-link

See How AbhiAndroid Step By Step Video Training Helps You Master Android App Development

Video Training - Unlock step by step video training with new content added regularly. Develop Android Apps.
Android App Source Code - Get amazing Ecommerce, Food Ordering and Ultimate WebView source code with documentation.
GET ACCESS NOW
close-link

With a very poor revenue from selling source code files or using Google AdSense, we need your help to survive this website. Please contribute any amount you can afford
Pay
* Terms & Conditions Apply
close-link