Home > Tricks & Tips > Auto-close dialog after a specific time

Auto-close dialog after a specific time

Not like Toast which is auto closed after 1-2 seconds, Dialog by default is not auto closed and doesn’t have any settings for auto closing.

In case you want your Dialog to auto-close after a time, then you may use a Timer to handle this task. Here a sample:

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

public class AlertDialogStudy extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        // get button
        Button btnShow = (Button)findViewById(R.id.btn_show);
        btnShow.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                builder.setTitle("Auto-closing Dialog");
                builder.setMessage("After 2 second, this dialog will be closed automatically!");

                final AlertDialog dlg = builder.create();


                final Timer t = new Timer();
                t.schedule(new TimerTask() {
                    public void run() {
                        dlg.dismiss(); // when the task active then close the dialog
                        t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
                }, 2000); // after 2 second (or 2000 miliseconds), the task will be active.


I guess it’s easy enough for you to understand, right?

Hope you learn something from it!



Pete Houston

  1. ilkin
    August 22, 2017 at 11:22 am

    thanks very

  2. Titan Deb
    April 12, 2016 at 2:32 pm

    how can i start activity after 5min and finish activity after 1 min continuously ….

  3. August 7, 2013 at 5:24 pm

    Thanks you…. It works

  4. PT
    July 3, 2013 at 9:03 pm

    Thanks … worked Great!

  5. February 25, 2013 at 5:08 pm

    Good one, Worked charm

  6. hiren
    December 14, 2012 at 6:24 pm


  7. joost
    September 14, 2012 at 1:00 pm

    Thanx good and effective.

  8. dn
    May 1, 2012 at 3:22 pm

    perfectly, thanks

  9. hami
    March 19, 2012 at 11:52 pm

    simple but effectively, thank you

  10. madfsa
    December 14, 2011 at 11:59 am


  11. Rodrigo
    October 27, 2011 at 1:13 am

    Tk´s a lot, perfect!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: