Android UI How to - Display a Progress Dialog








The following code shows how to display a progress dialog.

Example

package com.java2s.app;
/*from  w w w .j a  v  a2  s  .  c  o m*/
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ProgressDialog dialog = ProgressDialog.show(
                this, "Doing something", "Please wait...", true);
        new Thread(new Runnable() {
            public void run() {
                try {
                    //---simulate doing something lengthy---
                    Thread.sleep(5000);
                    dialog.dismiss();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
null




Note

To perform a long-running task in the background, you created a Thread using a Runnable block.

After the five seconds elapse, you dismiss the dialog by calling the dismss() method.