Input validation could be very painful. Building forms, in every technology, is a boring and painful job.
Fortunately on the web many libraries have born in order to facilitate this task to web developers. In android there are, built-in, some best practices you can use to facilitate the job but you’ll always have to take care about:
- in-depth data validation
- mandatory fields check
- error presentation
Bored about giving birth another painful form I decided to write an opensource library that does almost everything for me. The library is called FormEditText and you’ll be able to browse/download the sourcecode here in github.
The basic usage of the library will allow the developer to get an edit text validated through xml attributes.
In this snippet of xml code we have a lot of good tech ( both from Android and my library ):
- line #3: we define a new namespace. You’ll need to modify the package name ( com.andreabaccega.edittextformexample in the code ) with yours
- line #9: we provide an hint for the user so that he’ll know what he should write inside the field
- line #10: we provide a value for the stock android:inputType attribute.
- line #12: we tell the library that the field should be a valid email address
The last thing we’ll need to do is to ask the library to check the edittext validity:
This piece of code, performed when the user clicks the “submit” button, will ask the library to validate the content. Here two things could happen:
- the input is valid: in this case the library will just return true ( obviously )
- the input is not valid: in this case the library will return false and will set an error message that will look like the image on the top of the page.
When in the first case the library won’t do anything except returing “true” on line #3 of the previous snippet.
I’ll suggest to take a look at the github project. If you want to try the library I also setted up an example app that you can download from the market.
Link AppBrain | Link Android Market