android-logo

Android Progress Dialog


You must have seen an Android Progress Dialog in the UI of an Android app. It is a dialog box which, as the name suggests, shows the progress of a task. The simplest Android Progress Dialog shows a spinning wheel, also known as progress wheel. It can also have buttons and a progress bar that shows the loading status, but that requires some additional coding. A common example of an Android Progress Dialog is the dialog box you see while you are uploading or downloading a file.

If you are still unclear about what an Android Progress Dialog is, the following example of a typical dialog may help you:Open the MainActivity.java file, and add the following code in it:

MainActivity.java


public class MainActivity extends AppCompatActivity {

     ProgressBar progressBar;
     TextView textView;
     Button btDisplay;

     int i = 0;

    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        textView = (TextView) findViewById(R.id.textView);
        btDisplay = (Button)findViewById(R.id.btDisplay);
        btDisplay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i = progressBar.getProgress();
                new Thread(new Runnable() {
                    public void run() {
                        while (i < 100) {
                            i += 1;
                            handler.post(new Runnable() {
                                public void run() {
                                    progressBar.setProgress(i);
                                    textView.setText(i+"/"+progressBar.getMax());
                                }
                            });
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        });
    }
}

This code basically is the key to run our project as it is, in simpler words, the first screen to appear when our project is launched. Without this, performing other additional activities is not possible.Open your layout file, i.e. activity_main.xml, and enter the following code in it:

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="100dp"
        android:minHeight="50dp"
        android:minWidth="200dp"
        android:max="100"
        android:indeterminate="false"
        android:progress="0" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_gravity="center" />

    <Button
        android:id="@+id/btDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:p="GO" />

</LinearLayout>

This codes means that we have used the “ProgressBar” widget (to create a progress bar) and have added other features to it, such as a horizontal style, maximum value of 100, centralized p, minimum height of 50 dp, etc. We have also used the “LinearLayout” widget with a vertical orientation to arrange our contents vertically. Other than that, we have used the “TextView” widget, which will display p to the user, and the “Button” widget to create a button in the Progress Dialog that says “GO.”

Output
progress-dialog-1

previous-button
next-button

facebook-logo
twitter-logo
instagram-logo

2020 © All rights reserved.