For a custom AlertDialog, can I override the positive button to NOT close the dialog? instead I want to run some edit checks and keep the dialog open if my checks fail.
protected Dialog onCreateDialog(int id) {
Dialog alertDialog = null;
builder = new AlertDialog.Builder(this);
switch(id) {
case LOGIN_USERID_BLANK:
builder.setMessage((String)getString(R.string.username_not_blank));
builder.setPositiveButton((String)getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Can I do something here so that the dialog does not close?
}
});
break;
Here's a workaround until Google changes the Dialog API:
Ok, here is just an idea of how it is possible to implement.
AlertDialog.Builder
hassetView(View v)
method. So it is possible to add, say, a customLinearLayout
(inflated from resources right before Dialog building) with button(s). Then just set usualandroid.view.View.OnClickListener(s)
on the button(s). In this case don't use those "built-in/native" for theAlertDialog.Builder
buttons at all.Here's how I did it. Technically, it doesn't technically keep the dialog open, it closes it momentarily and re-opens it, but the net result is the same.
I faced the same issue wherein I was not able to stop dialog from getting dismissed even when the input I wanted to collected in dialog had validation issues. To resolve this issue, I added buttons in the dialog's custom view so that I have better control.
There does not seem to be clean way of stopping dialog from getting dismissed if you use
dialogBuilder's
setNeutralButton
orsetPositiveButton
orsetNegativeButton
.