Reputation: 163
I wanted to do a password alert box on an activity so once it has the correct answer it closes the dialog box but I cant seem to find a way when searching of how to close a dialog box the way I have coded it anyway.
Here is my code
final AlertDialog.Builder alert1 = new AlertDialog.Builder(this);
alert1.setTitle("Password");
alert1.setMessage("Please enter your password below and press Ok.");
final EditText input = new EditText(this);
alert1.setView(input);
alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString().trim();
((Global) Menu.this.getApplication()).setgPassword(value);
((Global) Menu.this.getApplication()).setgSiteId(strSavedMem1);
LogIn();
}
});
alert1.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
System.exit(0);
}
});
alert1.show();
Is there a way to close this alertbox?
Upvotes: 7
Views: 24756
Reputation: 6273
I know this question was asked a long time ago but I have a different solution from most of the other answers.
In my case, I wanted to cancel the AlertDialog when a certain button inside my inflated view was clicked. Of course, this has nothing to do with setNegativeButton() or setPositiveButton() so I had no access to any alertdialog object.
Here is how I solved this:
At the point of showing your AlertDialog (alertDialog.show()
), you can just store the .show
object into a variable as shown below:
AlertDialog shower = alertDialog.show();
Once this is done, you can easily cancel your alertdialog by calling shower.cancel()
anywhere within your AlertDialog scope.
I hope this helps. Merry coding!
Upvotes: 0
Reputation: 1
Try this:
final AlertDialog.Builder Dialog = new AlertDialog.Builder(this);
Dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Dialog.setCancelable(true);
}});
Upvotes: 0
Reputation: 470
You don´t have to do anything. As is said in the Android Documentation linked by anonymous: "When the user touches any of the action buttons created with an AlertDialog.Builder, the system dismisses the dialog for you." http://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog
Upvotes: 0
Reputation: 21117
You can receive dialog instance in DialogInterface onClick method.
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
Upvotes: 7
Reputation: 2057
Do you want the dialog to disappear when the user presses a button? If so, you don't have to do anything, it should be dismissed automatically (when the user presses any button).
Do you want the dialog to disappear on its own when the correct password has been entered? Then you will need to add a TextWatcher to the EditText field:
input.addTextChangedListener(new TextWatcher()....)
Callback functions in the TextWatcher will be called whenever the text changes, and you can there check whether the password is correct, and if it is, call
dialog.dismiss();
Upvotes: 0
Reputation: 3698
Check this,
AlertDialog.Builder successfullyLogin = new Builder(
YourActivity.this);
successfullyLogin.setCancelable(false);
successfullyLogin.setMessage("Successfully LoggedIn !");
successfullyLogin.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
}
});
successfullyLogin.show();
Upvotes: 0
Reputation: 3021
Try like this:
final AlertDialog.Builder alert1 = new AlertDialog.Builder(this);
alert1.setTitle("Password");
alert1.setMessage("Please enter your password below and press Ok.");
final EditText input = new EditText(this);
alert1.setView(input);
alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString().trim();
((Global) Menu.this.getApplication()).setgPassword(value);
((Global) Menu.this.getApplication()).setgSiteId(strSavedMem1);
LogIn();
alert1.dismiss();
}
});
alert1.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//System.exit(0);
alert1.cancel();
}
});
alert1.show();
}
Upvotes: 0
Reputation:
You should check out this link of the Android documentation: http://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog
There it is explained how can can cancel a Dialog.
Upvotes: 7