Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I close a ProgressDialog after a set time?

I am trying to close a ProgressDialog box automatically after 3 seconds. Here is the dialog:

ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Connecting");
progress.setMessage("Please wait while we connect to devices...");
progress.show();

I have tried a few methods, but I am unable to get any one of them to work. A simple time or anything would hopefully suffice. Thanks.

like image 529
Holdfast33 Avatar asked Mar 19 '15 19:03

Holdfast33


Video Answer


1 Answers

AsyncTask is okay if you are processing a long running task and then want to cancel it after, but it's a bit overkill for a 3 second wait. Try a simple handler.

    final ProgressDialog progress = new ProgressDialog(this);
    progress.setTitle("Connecting");
    progress.setMessage("Please wait while we connect to devices...");
    progress.show();

    Runnable progressRunnable = new Runnable() {

        @Override
        public void run() {
            progress.cancel();
        }
    };

    Handler pdCanceller = new Handler();
    pdCanceller.postDelayed(progressRunnable, 3000);

Updated adding show/hide:

progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        theLayout.setVisibility(View.GONE);
    }
});
like image 149
Gary Bak Avatar answered Oct 13 '22 18:10

Gary Bak