Android validating text input

Posted by / 16-Nov-2016 17:20

Well, if it can be done unobtrusively, the next best time to validate is when the user is actually entering the text (this is also where we can implement the cool icon and error message shown in the first screen).

To do this you have to implement a My flavor of it is shown below (I don’t pass the text separately to the validate method).

Android provides a long list of “input types” that you can set in the layout XML to “set the user up for success” so that they can only enter permissible characters or numbers.

For example: an input type of “phone” leads to only numbers, and characters such as -, (, ) being available for the user to enter: The XML to achieve this is shown below: The challenge is finding the proper list of input type values to use in the layout XML and what they make the Android device do. Next up: what you can do when the user changes the text in a field and still manages to enter something incorrect, even though we set them up for success above.

Since the Activity keeps track of the state of all of its component elements, this becomes an easy task.

Through this exercise, I was able to find some really great resources from all over the web that helped move the process forward.

I have consolidated what I learned here to help future Android developers down the path of validation.

to recognize a feature name but temporarily be unable to return its value.

Some feature values may be available only in specific contexts, such as before, during, or after a validation.

android validating text input-25android validating text input-35android validating text input-4

At first let’s define that Android form - it is a set of Edit Text’s whose data should be processed.