Skip to content Skip to sidebar Skip to footer

Android - Avoid Alertdialog To Close(error Handling)

May i know how to avoid AlertDialog to close even when i click the OK. The reason for this is to make a simple error handling when there is a wrong input. ------------------------

Solution 1:

Yes, you can simply override positive button functionality like this:-

builder.setPositiveButton("OK", null);
    AlertDialogdialog= builder.create();
    dialog .show();
    dialog .getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(newView.OnClickListener() {

                    @OverridepublicvoidonClick(View v) {
          // implement your code here
    });

Solution 2:

Create the Custom Listener Class First for Button Click Event as follows:

classCustomListenerimplementsView.OnClickListener {
privatefinal Dialog dialog;
publicCustomListener(Dialog dialog) {
    this.dialog = dialog;
}
@OverridepublicvoidonClick(View v) {

    // Do whatever you want here// If tou want to close the dialog, uncomment the line below//dialog.dismiss();
}

}

And when You are showing the Dialog or initialize the Dialog Button as follows :

AlertDialogdialog= dialogBuilder.create();
dialog.show();
Buttonbutton= dialog.getButton(DialogInterface.BUTTON_POSITIVE);
button.setOnClickListener(newCustomListener(dialog));

Post a Comment for "Android - Avoid Alertdialog To Close(error Handling)"