AlertDialog - How Can I Run Checks When User Hits 'OK'
Solution 1:
Here's a workaround until Google changes the Dialog API:
LayoutInflater factory = LayoutInflater.from(this);
final View view = factory.inflate(R.layout.yoMamma, null);
final Dialog dlg = new AlertDialog.Builder(this)
.setTitle(R.string.yoTitle)
.setView(view)
.setPositiveButton(R.string.dlgOK, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
// This won't be called.
}})
.create();
dlg.show();
View button = ((AlertDialog)dlg).getButton(DialogInterface.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(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.
class MyAlertDialog implements OnDismissListener, OnCancelListener {
final private EditText editText;
final private AlertDialog alertDialog;
final private EventManager eventManager;
final private CategorySelector categorySelector;
private Boolean canceled;
MyAlertDialog(Context context) {
editText = new EditText(context);
alertDialog = buildAlertDialog(context);
alertDialog.setOnDismissListener(this);
alertDialog.setOnCancelListener(this);
show();
}
private AlertDialog buildAlertDialog(Context context) {
return new AlertDialog.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();
}
public void show() {
canceled = false;
alertDialog.show();
}
@Override
public void onDismiss(DialogInterface dialog) {
if(!canceled) {
final String name = editText.getText().toString();
if(name.equals("")) {
editText.setError("Please enter a non-empty name");
show();
} else {
doWhateverYouWantHere(name);
}
}
}
@Override
public void onCancel(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'"