0

I've added input validation to a set of edit texts in a dialog message using the setError method in Android.

But when I test it by leaving some of the edit texts blank and clicking "ok" on the dialog, the dialog closes without triggering a prompt to enter values.

Can anyone see what is wrong with this implementation?

I think it may be something to do with a call to close the dialog before input validation is triggered.

This is how I've set the input validation based on the 4th answer in this question, Check if EditText is empty.:

    else if(TextUtils.isEmpty(strColour)) {
        colourText.setError("Please enter a value");
        return;
     }
}
   dialog.cancel(); 
}
Community
  • 1
  • 1
Brian Var
  • 6,029
  • 25
  • 114
  • 212
  • Why are you setting error in only one edit text `shipText` irrespective of the error location? Copy-paste error probably! – Rohit5k2 Jan 05 '15 at 21:23
  • @Rohit5k2, that was an editing error, the dialog still does not stay open for validation though, if I click "ok" , it still lets the dialog close and doesn't trigger the input validation. – Brian Var Jan 05 '15 at 21:29

1 Answers1

1

That's because you are calling dialog.cancel() irrespective of error(s). Call this method only if there is no error. Keep a track of error using a boolean. If error exist make it false otherwise keep it true and call the dialog.cancel() method only if the boolean is true.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • See the code in my answer, it would give you and idea. – Rohit5k2 Jan 05 '15 at 21:48
  • You could create your own custom dialog with a submit button which you can set onClickListener on it then validate when a user clicks it. – Eenvincible Jan 05 '15 at 21:57
  • There are some changes you need to make in your code to make it work. This will help you achieve that http://stackoverflow.com/questions/4016313/how-to-keep-an-alertdialog-open-after-button-onclick-is-fired – Rohit5k2 Jan 05 '15 at 22:04
  • Thats because of default listener. See the link in my last comment. – Rohit5k2 Jan 06 '15 at 00:34
  • Accepted, but this solution will only work on a custom dialog not my implementation. – Brian Var Jan 09 '15 at 17:31