Tutorials  /  JavaScript

Maximizing User-Friendliness: Understanding Android ProgressDialog

Ccentron Redaktion · September 2024 ·3 min read ·JavaScript, Tutorial

Dive into the realm of Android development and learn how to effectively display progress using the ProgressDialog. Our blog post explains the difference between ProgressDialog and ProgressBar and provides a step-by-step guide for implementation.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Android ProgressDialog

Android ProgressDialog is an extension of AlertDialog. To learn more about an AlertDialog, check out the tutorial here. An Android ProgressDialog is a dialog/window that displays the progress of a task. An Android ProgressDialog is almost identical to a ProgressBar, except it is displayed as a dialog. To create a ProgressDialog displaying a ProgressBar, we need to instantiate it as follows:

Code
ProgressDialog progress = new ProgressDialog(this);

Difference between Android ProgressDialog and ProgressBar

1. ProgressBar is a view (like TextView, ImageView, Button, etc.) that can be used in the layout to display progress. A ProgressBar is used to indicate that something is still loading in the app while the user may still interact with other parts.

2. ProgressDialog is a dialog with a built-in ProgressBar. A ProgressDialog is used when we want to prevent the user from interacting with the application while waiting. The dialog aspect prevents the user from doing anything until it is dismissed.

Attributes of Android ProgressDialog

Some important attributes of Android ProgressDialog are listed below:

1. setMessage(): This method is used to display the message for the user. Example: Loading...

2. setTitle(): This method is used to set a title for the dialog.

3. setProgressStyle(ProgressDialog.STYLE_HORIZONTAL): This method is used to display the horizontal progress bar in the dialog.

4. setProgressStyle(ProgressDialog.STYLE_SPINNER): This method is used to display the circular/spinning progress bar in the dialog.

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 the progress dialog.

9. incrementProgressBy(int diff): This method increments the progress display by the value passed as a parameter.

Project Structure of Android ProgressDialog

The activity_main.xml contains a button that calls a ProgressDialog upon click, as shown in the following XML code:

Code
<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 as follows:

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

public class MainActivity extends AppCompatActivity {

    Button button;
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMax(100);
                progressDialog.setMessage("Loading...");
                progressDialog.setTitle("ProgressDialog Example");
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.show();
                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();
            }

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

In this tutorial, we are developing an application that contains a ProgressDialog containing a horizontal ProgressBar that increments every 200 milliseconds.

That concludes our guide to Android ProgressDialog with an example. With this information, you are ready to implement progress displays in your Android applications. If you have any questions, feel free to let us know!

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren