How to dismiss AlertDialog in android
Actually there is no any cancel()
or dismiss()
method from AlertDialog.Builder Class.
So Instead of AlertDialog.Builder optionDialog
use AlertDialog
instance.
Like,
AlertDialog optionDialog = new AlertDialog.Builder(this).create();
Now, Just call optionDialog.dismiss();
background.setOnClickListener(new OnClickListener() { public void onClick(View v) { SetBackground(); // here I want to dismiss it after SetBackground() method optionDialog.dismiss(); }});
I think there's a simpler solution: Just use the DialogInterface
argument that is passed to the onClick
method.
AlertDialog.Builder db = new AlertDialog.Builder(context); db.setNegativeButton("cancel", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface d, int arg1) { db.cancel(); //here db.cancel will dismiss the builder }; });
See, for example, http://www.mkyong.com/android/android-alert-dialog-example.
Try this:
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog OptionDialog = builder.create(); background.setOnClickListener(new OnClickListener() { public void onClick(View v) { SetBackground(); OptionDialog .dismiss(); } });