Progress Dialog

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.


Note: As ProgressDialog has been depreciated so will not recommend to use ProgressDialog. If you required to use this, we will implemente ProgressDialog using AlertDialog as below:

public void showProgrssDialog(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setView(R.layout.progress_dialog);

    AlertDialog dialog = builder.create();
    dialog.show();

    Window window = dialog.getWindow();
    if (window != null) {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.copyFrom(dialog.getWindow().getAttributes());
        layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        dialog.getWindow().setAttributes(layoutParams);
    }
}


progress_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_margin="10dp"
        android:layout_height="wrap_content"/>

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tvMsg"
        android:layout_width="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="10pt"
        android:text="Loading..."
        android:layout_height="wrap_content"/>
</androidx.appcompat.widget.LinearLayoutCompat>


File Downloading ProgressBar:

public void showHorizontalProgrssDialog(View view) {
    LayoutInflater inflater = getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.horizontal_progress_dialog, null, false);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setView(dialogView);

    final AppCompatTextView tvMsg = dialogView.findViewById(R.id.tvMsg);

    final ContentLoadingProgressBar progressBar = dialogView.findViewById(R.id.progressBar);
    progressBar.setMax(200);
    tvMsg.setText("Downloading " + 0 + "%");

    final AlertDialog dialog = builder.create();
    dialog.show();

    Window window = dialog.getWindow();
    if (window != null) {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.copyFrom(dialog.getWindow().getAttributes());
        layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        dialog.getWindow().setAttributes(layoutParams);
    }

    final Handler handler = new Handler();
    new Thread(new Runnable() {
        private int progress = 0;

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }

                final int max = progressBar.getMax();
                progress = progress + (int) (progressBar.getMax() * .1);

                if (progress >= progressBar.getMax()) {
                    dialog.dismiss();
                    break;
                }

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        int percent = (int) (progress * .5);
                        progressBar.setProgress(progress);
                        tvMsg.setText("Downloading " + percent + "%");
                    }
                });
            }
        }
    }).start();

}


horizontal_progress_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:orientation="vertical">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tvMsg"
        android:layout_width="wrap_content"
        android:layout_gravity="center_vertical"
        android:scrollHorizontally="true"
        android:textSize="10pt"
        android:text="Loading..."
        android:layout_height="wrap_content"/>

    <androidx.core.widget.ContentLoadingProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_centerInParent="true" />

</androidx.appcompat.widget.LinearLayoutCompat>