EasyForm — painless form creation and field validation in Android
I have worked on a form in Android a few times and realized that it is quite a work despite how it looks on screen. In Android, to do field validation as user types, you would add a TextWatcher to each field and have validation logic for each field. To disable a button while a form does not have all valid inputs and enable it when all fields have valid inputs, you would track for each EditText’s validation condition. A lot of boilerplate. A lot of logics…
Then a bit bit of Angular part of me jumped in. Even for a beginner in AngularJS, the form creation and field validation in Angular was very neat and clear. Let’s take a simple example.
Setup a directive to toggle an error class when a validation fails.

I am sure that an Angular expert can tell this is not the best way to show an error view. Yet once I have this directive, the rest is simple. I add a keyword such as “required”, “ng-pattern”, “min” or “ng-maxlength” to an input tag.

Now, this form toggle an error view when an input field has an invalid text. The first input will show an error when there is no texts and the second input will show an error when a text is not a valid email format. It is scalable and does not require any more JavaScript logic.
So, what wouldn’t I have the similar thing in Android?
EasyForm

I published the EasyForm in jcenter last week. This is a project to achieve form creation and validation as simple as possible. It allows you to add all error properties just in xml and no Java logic is needed for field validation.
Example

EasyForm
app:submitButton=”@+id/submit_button”
Add this to EasyForm will track all child fields and see whether any of the child fields have invalid or unfilled input. A button with the ID you set for “submitButton” will be disabled while there are invalid or unfilled field and enabled when all fields are filled without any errors.

EasyFormEditText / EasyTextInputLayout
EasyFormEditText or EasyTextInputLayout is where the most of the magic happens, the one you set error properties. These should be children of EasyForm (can be nested).
You will add only a few properties for each field, an error message and an error criteria or two.
Again, no Java logic needed.
Then, an error will be displayed when an input does not match with a criteria you add for EasyFormEditText or EasyTextInputLayout and dismiss the error when you correct to a valid one.
To make a field required, add these properties.
app:errorMessage="Please fill this field"
app:errorType="empty"

To make a field allow digit only,
app:errorMessage="Please input digit only"
app:regexPattern="[0-9]+"

To make a field only allow inputs with a length between 6 and 12,
app:errorMessage="Account should be 6-12 chars"
app:minChars="6"
app:maxChars="12"

These are a very minimum set of form validation functionality, however, with this set, it saves you lots of java code.
See it in action?
Download and how to instructions are documented on Github: https://github.com/emmasuzuki/EasyForm
Try it out and feel free to open issues and help me grow this project.