Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss AlertDialog.Builder?

In the following code below, how do I dismiss the alert box? I don't want to cause a memory leak. I tried the .dismiss() on alertDialog, but that didn't work... Thanks

// User pressed the stop button public void StopMsg_button_action(View view){     final EditText password_input = new EditText(this); // create an text input field     password_input.setHint("Enter Password"); // put a hint in it     password_input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); // change it to password type      AlertDialog.Builder alertDialog = new Builder(this); // create an alert box     alertDialog.setTitle("Enter Password"); // set the title     alertDialog.setView(password_input);  // insert the password text field in the alert box     alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // define the 'OK' button         public void onClick(DialogInterface dialog, int which) {              String entered_password = password_input.getText().toString();              if (entered_password.equals(my_password)) {                 locManager.removeUpdates(locListener); // stop listening for GPS coordinates                 startActivity(new Intent(Emergency_1Activity.this,Main_MenuActivity.class)); // go to main menu              } else {                  alert("Incorrect Password");              }         }      });     alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { // define the 'Cancel' button         public void onClick(DialogInterface dialog, int which) {          }      });     alertDialog.show(); // show the alert box } 
like image 335
sneaky Avatar asked Jul 01 '12 20:07

sneaky


People also ask

How do I dismiss Materialalertdialogbuilder?

setCancelable(false); AlertDialog dialog = builder. show(); In order to dismiss the dialog, you can call dismiss function like this.

How do I dismiss all dialogs in Android?

I use onCreateDialog(int id) to create each dialog and I use showDialog(int id) and dismissDialog(int id) method show and dismiss each dialog respectively.

What is AlertDialog builder in Android?

Alert Dialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response the next step is processed. Android Alert Dialog is built with the use of three fields: Title, Message area, Action Button.


1 Answers

What didn't work about dismiss()?

You should be able to use either Dialog.dismiss(), or Dialog.cancel()

alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { // define the 'Cancel' button     public void onClick(DialogInterface dialog, int which) {         //Either of the following two lines should work.         dialog.cancel();         //dialog.dismiss();     }  }); 
like image 161
FoamyGuy Avatar answered Sep 22 '22 06:09

FoamyGuy