setMessage( "Select yes to display toast message and no to dismiss the dialog ?" ) // button and setting text to it. // and setting text to it. // show to display our dialog.
You can do this very easily. AlertDialog. Builder alertDialogBuilder = new AlertDialog. Builder(context); // set title alertDialogBuilder.
Set the onClickListener during the creation of the dialog to null. Then set a onClickListener after the dialog is shown.
setPositiveButton(int textId, DialogInterface.OnClickListener listener) Set a listener to be invoked when the positive button of the dialog is pressed. AlertDialog.Builder. setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
Curiosity question here.
I use a lot of dialogs builders and most of the time my negative cancel button do nothing except dismiss the dialog. The code I found everywhere on the web is this :
builder.setNegativeButton(
"cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
I happened to find out that this code do exactly the same :
builder.setNegativeButton("cancel", null);
So my question is then : is that a bad habit not to manually dismiss the dialog, and if yes why ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With