3

I'm displaying an DialogFragment prompting for the user to provide input. I'd like to validate this input (should not be empty) and display a Toast if the validation fails, without dismissing the dialog.

Currently, I display a Toast when the validation fails, but the dialog gets dismissed. How do I prevent this?

DialogFragment class:

public class DialogFragmentAddNewFolder extends DialogFragment implements DialogInterface.OnClickListener {

    private EditText etName;
    private DialogFragmentAddNewFolderListener mListener;

    public interface DialogFragmentAddNewFolderListener {
        public void onAddNewFolderPositiveClick(Folder folder);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View ourView = inflater.inflate(
                R.layout.dialogfragment_add_note, null);
        builder.setView(ourView);
        etName = (EditText) ourView.findViewById(R.id.etAddFolder);

        builder.setTitle(R.string.dialogfragment_add_folder_title);

        builder.setPositiveButton(
                R.string.dialogfragment_add_folder_pos_btn, this);
        builder.setNegativeButton(
                R.string.dialogframent_add_folder_neg_btn, this);
        return builder.create();
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case DialogInterface.BUTTON_POSITIVE:
                String name = etName.getText().toString();
                if (name.equals("")) {
                    Toast.makeText(getActivity(), "Provide a name", Toast.LENGTH_SHORT).show();
                } else {
                    Folder folder = new Folder();
                    folder.setName(name);
                    mListener.onAddNewFolderPositiveClick(folder);
                }
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                dialog.dismiss();
                break;
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (DialogFragmentAddNewFolderListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + "Must implement DialogFragmentAddNewFolderListener.");
        }
    }
}
Marcus
  • 6,697
  • 11
  • 46
  • 89
  • 1
    possible duplicate of [How to prevent a dialog from closing when a button is clicked](http://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked) – Dan Lew Jan 20 '15 at 19:46

1 Answers1

0

Try applying a text change listener to your EditText and use InputFilter on that listener. Then you can do your validation logic in that filter.

In your case you might not need that filter at all, where you can simply do the checking in the listener logic. e.g.

yourEditText.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        doYourChecking();
    } 

});
StoneBird
  • 1,900
  • 13
  • 12