Harinder
Harinder

Reputation: 11944

How to Cancel a AlertDialog.Builder

What Should i Write in the Runnable Run Method to cancel the Alert.Bulider??

AlertDialog.Builder ad;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Context context=this;
     ad = new AlertDialog.Builder(context);
        ad.setTitle("Warning");
        ad.setMessage("Just Testing It");

        ad.setPositiveButton("Yes", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });

        ad.setNegativeButton("Nooooo", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });

        ad.show();
        Handler h=new Handler();
        h.postAtTime(r, 10000);

    }
    public Runnable r=new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

        }
    };

Upvotes: 18

Views: 22651

Answers (3)

Van Huy
Van Huy

Reputation: 11

I just set null for listener, it works for me.

ad.setNagativeButton(yourMessage, null);

Hope it helps you.

Upvotes: 0

Amir Ansari
Amir Ansari

Reputation: 209

show() returns AlertDialog so make a variable as below:

AlertDialog dlg = ad.show();

and then dismiss when required -

dlg.dismiss();

Upvotes: 20

pankajagarwal
pankajagarwal

Reputation: 13582

You cannot hide a AlertDialog.Builder. Instead declare the member variable ad as AlertDialog, create the AlertDialog using the builder and assign it to ad by writing ad = builder.create(). In the run method call ad.cancel();

Upvotes: 25

Related Questions