You can now find me blogging at www.theflyingandroid.com
Over the weekend I have been working on a small library to help validating the input into form fields in Android. I have open sourced it on Github and I am in the process of deploying it to Maven.
This library was born from some self-inflicted validation frustration in a project at work and jealousy at one of my colleagues better solution in the iOS department.
What does it do?
The library provides a class named InputValidator that can be wrapped around any object that extends from TextView. The InputValidator will validate the value of the input against a set of Validators attached to it. The InputValidator sets an OnFocusListener that validates the input when the user leaves the input and it uses a TextWatcher to remove the error when the user has started typing again.
The Validator is a simple interface with two methods. The first is boolean validate(String) this is where any validation logic will happen and will return true if the input is valid and false if it is invalid. The second method if String getValidationMessage() which returns a message to display if there has been a validation error.