Display a Progress Dialog
Description
The following code shows how to display a progress dialog.
Example
package com.java2s.app;
// w ww .j a v a 2 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();
}
}
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.