Alertdialog - How Can I Run Checks When User Hits 'ok'
Solution 1:
Here's a workaround until Google changes the Dialog API:
LayoutInflaterfactory= LayoutInflater.from(this);
finalViewview= factory.inflate(R.layout.yoMamma, null);
finalDialogdlg=newAlertDialog.Builder(this)
.setTitle(R.string.yoTitle)
.setView(view)
.setPositiveButton(R.string.dlgOK, newDialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialog, int which) {
// This won't be called.
}})
.create();
dlg.show();
Viewbutton= ((AlertDialog)dlg).getButton(DialogInterface.BUTTON_POSITIVE);
button.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
// This WILL be called.// Remove the following if you don't want the dialog to dismiss
dlg.dismiss();
}});
Solution 2:
Ok, here is just an idea of how it is possible to implement.
AlertDialog.Builder
has setView(View v)
method. So it is possible to add, say, a custom LinearLayout
(inflated from resources right before Dialog building) with button(s). Then just set usual android.view.View.OnClickListener(s)
on the button(s). In this case don't use those "built-in/native" for the AlertDialog.Builder
buttons at all.
Solution 3:
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.
classMyAlertDialogimplementsOnDismissListener, OnCancelListener {
finalprivate EditText editText;
finalprivate AlertDialog alertDialog;
finalprivate EventManager eventManager;
finalprivate CategorySelector categorySelector;
private Boolean canceled;
MyAlertDialog(Context context) {
editText = newEditText(context);
alertDialog = buildAlertDialog(context);
alertDialog.setOnDismissListener(this);
alertDialog.setOnCancelListener(this);
show();
}
private AlertDialog buildAlertDialog(Context context) {
returnnewAlertDialog.Builder(context)
.setTitle(context.getString(R.string.enter_name))
.setMessage(context.getString(R.string.enter_name))
.setView(editText)
.setNeutralButton(context.getString(R.string.save_text), null)
.setNegativeButton(context.getString(R.string.cancel_text), null).create();
}
publicvoidshow() {
canceled = false;
alertDialog.show();
}
@OverridepublicvoidonDismiss(DialogInterface dialog) {
if(!canceled) {
finalStringname= editText.getText().toString();
if(name.equals("")) {
editText.setError("Please enter a non-empty name");
show();
} else {
doWhateverYouWantHere(name);
}
}
}
@OverridepublicvoidonCancel(DialogInterface dialog) {
canceled = true;
}
}
Solution 4:
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
or setPositiveButton
or setNegativeButton
.
Post a Comment for "Alertdialog - How Can I Run Checks When User Hits 'ok'"