0

I have a dialog fragment A with several input control elements. When the user enters an invalid value for some field, a dialog box B with validation error message pops up.

The problem is that when I dismiss dialog B, dialog A is not visible. This is not user-friendly - if the dialog box has 10 values and the user entered correct values for 9 of them, then in case of a validation error he/she has to re-enter those 9 correct values again.

Both A and B are implemented using AlertDialog.

How can I implement display of validation error messages such that the user can return to the dialog A, correct wrong values and press the OK button again?

Glory to Russia
  • 17,289
  • 56
  • 182
  • 325
  • I don't think that you can keep dialog on screen when user clicked on dialog button........ SO better and better way to use Activity as an AlertDialog.... this will be very easy – Pankaj Kumar May 03 '13 at 09:24
  • 1
    @Dmitri Pisarenko-1 dialog displays at a time only..so you should store the values of dialog A fields and dismiss it.After that when dialog B opens and when press OK of dialog B -restore the fields value to the dialog A. – TheFlash May 03 '13 at 09:32
  • @PankajKumar I can't make it an activity because the parent of the dialog is a fragment. – Glory to Russia May 03 '13 at 12:29
  • So I would suggest you http://stackoverflow.com/questions/7593415/show-fragment-as-a-dialog-or-as-a-usual-activity – Pankaj Kumar May 03 '13 at 12:39
  • Thanks. Is there any other means to tell the user that the value of control X is wrong (like highlighting it, for example) ? – Glory to Russia May 04 '13 at 07:31

1 Answers1

1

This is kludge-y, but I've managed to get it working for me. The way you do this, show validation errors on an alert dialog (and prevent the dialog from being dismissed) is to override the OnClickListener for the dialog button. You may be thinking "I'm already doing that... thanks jerk", but that's the trick. You're probably calling AlertDialog.Builder().setTitle().setView().setPositiveButton(title, new OnClickListener() {...validation logic...}).create(); and then returning that. If you want to prevent the dialog from being dismissed, you need to add an OnShowListener to the dialog(after it's been created) and override the button click there. Here's an example:

Way that doesn't work:

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Dialog example").setPositiveButton(android.R.string.ok, new Dialog.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // do some validation logic
            if (!valid) {
                myEditText.setError(R.string.invalid_value); //
            } else {
                dialog.dismiss();
            }
        }
    });

    Dialog dialog = builder.create();
    // show the dialog, or return it, whatever you're going to do with it

So the above doesn't work because your onClickListener is called before the default one is, which will dismiss the dialog. Here's one way around it:

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Dialog example").setPositiveButton(android.R.string.ok, null);

    Dialog dialog = builder.create();
dialog.setOnShowListener(new OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {
        ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // do some validation logic
            if (!valid) {
                myEditText.setError(R.string.invalid_value); //
            } else {
                dialog.dismiss();
            }
        }
    }
});

Just remember, you're responsible for dismissing it!

Travis
  • 3,737
  • 1
  • 24
  • 24