Validating Input

You've created your form so that users can enter information, and perhaps you've already created the mechanism to save the content to a database or remote server. But what happens when the user enters invalid text or no text? This is where input validation enters the picture.

Input validation validates the input before the save takes place. Assume that the user does not enter text for the title or the message and attempts to save; should she be allowed to save? Of course not!

Unfortunately, a built-in Android validation framework does not exist. Hopefully, in future versions of the Android platform this feature will be introduced. However, you have ways to validate input with the current framework.

The method in which you provide validation to the user is up to you. Here are some common methods in which I've seen developers implement validation:

  • TextWatcher: Implement a TextWatcher on the EditText widget. This class provides callbacks to you each time the text changes in the EditText widget. Therefore, you can inspect the text on each keystroke.
  • On Save: When the user attempts to save the form that he is working with, inspect all the form fields at that time and inform the user of any issues found.
  • onFocusChanged(): Inspect the values of the form when the onFocusChanged() event is called — which is called when the view has focus and when it loses focus. This is usually a good place to set up validation.

The Task Reminder application does not provide input validation; ...

Get Android™ Tablet Application Development For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.