Add a delay to Progress Dialog

Sapp picture Sapp · Nov 17, 2010 · Viewed 32.6k times · Source

I want to make a dummy progress dialog appear for 2 or 3 seconds. It won't actually do anything other than say detecting. I have the code:

    ProgressDialog dialog = ProgressDialog.show(this, "", "Detecting...",
            true);
    dialog.show();

    dialog.dismiss();

But what do I put in between the show, and the dismissal to have the dialog appear for a few seconds? Thanks!

Answer

Peter Knego picture Peter Knego · Nov 17, 2010

The correct way - it does not block your main thread, so UI stays responsive:

dialog.show();

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
        dialog.dismiss();
    }
}, 3000); // 3000 milliseconds delay