Reputation: 107
I have a dialog box prompt for the user and the user input is to be used in the next step of the code. I am trying to use OnDismiss to make the rest of the code take in user input. Below is the code snippet:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input = 3;
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input = 1;
}
});
alert.show();
}
public void OnDismiss(DialogInterface dialogInterface)
{
Toast.makeText(this, "Input: " + input, Toast.LENGTH_LONG)
.show();
}
However, when I press the dialog box button, the dialog box is dismissed but the toast does not appear. Please suggest what am I doing wrong here.
Upvotes: 0
Views: 168
Reputation: 506
you have to use dialog.dismiss();
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input = 3;
dialog.dismiss();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input = 1;
dialog.dismiss();
}
});
Upvotes: 0
Reputation: 2550
Set the OnDismissListener
to your AlertDialog
first then call dismiss()
when dialog button is pressed, code as bellowing:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input = 3;
dialog.dismiss();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input = 1;
dialog.dismiss();
}
});
alert.setOnDismissListener(new OnDismissListener(){
public void OnDismiss(DialogInterface dialogInterface)
{
Toast.makeText(this, "Input: " + input, Toast.LENGTH_LONG)
.show();
}
});
alert.show();
Upvotes: 1