Add a delay to Progress Dialog
Asked Answered
B

6

16

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!

Batory answered 17/11, 2010 at 21:19 Comment(0)
S
62

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
Stator answered 17/11, 2010 at 21:33 Comment(2)
Great answer, just you are missing an additional curly brace before the last line: } }, 3000); // 3000 milisecondsTyndale
excuse me! how we can update our message on progress dialog while waiting. for example: 5 sec... 4 sec... 3 sec... 2 sec... 1 sec... BOOOOM! and dismissCockney
K
3
        progress.setProgress(100);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                pdialog.dismiss();
            }}, 3000);
Kickstand answered 3/9, 2012 at 14:59 Comment(0)
E
2

You can also use CountDownTimer of Android which is much more efficient than any other solution posted and it also support fires on regular interval through its onTick() method.

Have a look at this example,

 new CountDownTimer(3000, 1000) {

     public void onTick(long millisUntilFinished) {
             // You don't need anything here
     }

     public void onFinish() {
         dialog.dismiss();
     }
  }.start();
Elviraelvis answered 23/1, 2015 at 13:14 Comment(1)
This is exactly what i need, Hanlder funtion can not use in Leanback :DNickolas
P
1

You can do something like this:

new AsyncTask<Void, Void, Void>
{
    ProgressDialog dialog = new ProgressDialog(MainActivity.this);
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        dialog.setTitle("Please wait");
        dialog.setMessage("Preparing resources...");
        dialog.setCancelable(false);
        dialog.show();
    }


    @Override
    protected Void doInBackground(Void... params) {
        try{
           Thread.sleep(3000);
        } 
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPostExecute(Void aVoid) 
    {
        super.onPostExecute(aVoid);
        if (dialog!=null && dialog.isShowing())
        {
            dialog.dismiss();
        }
    }
}.execute();
Passade answered 20/12, 2015 at 18:56 Comment(0)
C
0

Nothing comes between dismiss and show.. the time depending upon you. For example dialog will show before server access and it will dismiss getting result means

 dialog.show();
 ServerCall();
 dialog.close();

If you need empty delay means then use CountDownTimer call between that..

Cloakroom answered 17/11, 2010 at 21:26 Comment(0)
B
0

In case some one looking for Xamarin.Android (using C#) solution (of Peter Knego's answer), here is how:

new Handler().PostDelayed(() => { 
    dialog.dismiss();
}, 1000);
Blackthorn answered 16/4, 2018 at 5:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.