Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way prevent AlertDialog from closing with invalid inputs?

I am using a simple text field alert dialog with a positive and a cancel button. I want to validate my alert dialog and prevent the done button from closing the AlertDialog if the input is invalid.

Is there any way short of creating a custom dialog to prevent the PositiveButton onClick() handler from closing the dialog if the validation fails?

class CreateNewCategoryAlertDialog {
    final EditText editText;
    final AlertDialog alertDialog;

    class PositiveButtonClickListener implements OnClickListener {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String name = editText.getText().toString();
            if(name.equals("")) {
                editText.requestFocus();
                editText.setError("Please enter a name");
                // Some code to stop AlertDialog from closing goes here...
            } else {
                doSomethingUsefulWithName();
            }
        }
    }

    AlertDialog buildAlertDialog(Context context) {
        return new AlertDialog.Builder(context)
        .setTitle(context.getString(R.string.enter_name))
        .setMessage(context.getString(R.string.enter_name_msg))
        .setView(editText)
        .setPositiveButton(context.getString(R.string.done), new PositiveButtonClickListener())
        .setNegativeButton(context.getString(R.string.cancel), null).create();
    }
}
like image 836
Jeff Axelrod Avatar asked Jun 28 '11 18:06

Jeff Axelrod


People also ask

How can I stop a dialog from closing when a button is clicked Kotlin?

If you wish to prevent a dialog box from closing when one of these buttons is pressed you must replace the common button handler for the actual view of the button. Because it is assigned in OnCreate(), you must replace it after the default OnCreate() implementation is called.

Can you have an alert dialog without any buttons if not why?

You can do this very easily. AlertDialog. Builder alertDialogBuilder = new AlertDialog. Builder(context); // set title alertDialogBuilder.

How do I turn off AlertDialog?

AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.

Which method is used to set the alert AlertDialog?

Alert Dialog code has three methods: setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is use to set the icon on Alert dialog box.


1 Answers

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;
    }
}
like image 127
Jeff Axelrod Avatar answered Oct 06 '22 21:10

Jeff Axelrod