Android – Form validation using AwesomeValidation

In most of the time we have forms in our Android apps like, Contact form, Survey forms, Signup forms, Login forms, Order forms, etc. And for all those forms we also write validation logic to validate the entered data. But writing those validation logic is cumbersome, tedious and also add lots of boiler plat code. To solve this there is an amazing library available in Android i.e, AwesomeValidation


To setup AwesomeValidation, we need to add this library as a dependency in build.gradle.

//Using JCenter
dependencies {
    implementation 'com.basgeekball:awesome-validation:4.1'

Alternatively it is also available in Jitpack

  • Add it in your root build.gradle at the end of repositories:
allprojects {
    repositories {
        maven { url '' }
  • Add the dependency
dependencies {
    implementation 'com.github.thyrlian:AwesomeValidation:v4.1'
    // you can also use the short commit hash to get a specific version
    // implementation 'com.github.thyrlian:AwesomeValidation:GIT_COMMIT_HASH'


Supported Form Widgets

AwesomeValidation supports range of form widgets those are as follows –

  • EditText
  • Spinner
  • TextInputLayout

Read also –

Validation Rules

AwesomeValidation supports following validation rules –

  • String
  • Numeric
  • AlphaNumeric
  • IP Address
  • Web Url
  • Phone Number
  • Year
  • Any custom REGEX

How to use

Using AwesomeValidation is also very simple here is the following code snippet to validate –

  • STEP 1
// Step 1: designate a style
AwesomeValidation mAwesomeValidation = new AwesomeValidation(BASIC);

// or
AwesomeValidation mAwesomeValidation = new AwesomeValidation(COLORATION);
mAwesomeValidation.setColor(Color.YELLOW);  // optional, default color is RED if not set
// or
AwesomeValidation mAwesomeValidation = new AwesomeValidation(UNDERLABEL);
mAwesomeValidation.setContext(this);  // mandatory for UNDERLABEL style
// setUnderlabelColor is optional for UNDERLABEL style, by default it's holo_red_light
mAwesomeValidation.setUnderlabelColorByResource(android.R.color.holo_orange_light); // optional for UNDERLABEL style
mAwesomeValidation.setUnderlabelColor(ContextCompat.getColor(this, android.R.color.holo_orange_dark)); // optional for UNDERLABEL style
// or
AwesomeValidation mAwesomeValidation = new AwesomeValidation(TEXT_INPUT_LAYOUT);
mAwesomeValidation.setTextInputLayoutErrorTextAppearance(; // optional, default color is holo_red_light if not set
// by default, it automatically sets focus to the first failed input field after validation is triggered
// you can disable this behavior by
  • STEP 2
// Step 2: add validations
// support regex string, java.util.regex.Pattern and Guava#Range
// you can pass resource or string
mAwesomeValidation.addValidation(activity,, "[a-zA-Z\\s]+", R.string.err_name);
mAwesomeValidation.addValidation(activity,, RegexTemplate.TELEPHONE, R.string.err_tel);
mAwesomeValidation.addValidation(activity,, android.util.Patterns.EMAIL_ADDRESS, R.string.err_email);
mAwesomeValidation.addValidation(activity,, Range.closed(1900, Calendar.getInstance().get(Calendar.YEAR)), R.string.err_year);
mAwesomeValidation.addValidation(activity,, Range.closed(0.0f, 2.72f), R.string.err_height);
// or
mAwesomeValidation.addValidation(editText, "regex", "Error info");

// to validate TextInputLayout, pass the TextInputLayout, not the embedded EditText
AwesomeValidation mAwesomeValidation = new AwesomeValidation(TEXT_INPUT_LAYOUT);
mAwesomeValidation.addValidation(activity,, Patterns.EMAIL_ADDRESS, R.string.err_email);

// to validate the confirmation of another field
String regexPassword = "(?=.*[a-z])(?=.*[A-Z])(?=.*[\\d])(?=.*[~`!@#\\$%\\^&\\*\\(\\)\\-_\\+=\\{\\}\\[\\]\\|\\;:\"<>,./\\?]).{8,}";
mAwesomeValidation.addValidation(activity,, regexPassword, R.string.err_password);
// to validate a confirmation field (don't validate any rule other than confirmation on confirmation field)
mAwesomeValidation.addValidation(activity,,, R.string.err_password_confirmation);

// to validate with a simple custom validator function
mAwesomeValidation.addValidation(activity,, new SimpleCustomValidation() {
    public boolean compare(String input) {
        // check if the age is >= 18
        try {
            Calendar calendarBirthday = Calendar.getInstance();
            Calendar calendarToday = Calendar.getInstance();
            calendarBirthday.setTime(new SimpleDateFormat("dd/MM/yyyy", Locale.US).parse(input));
            int yearOfToday = calendarToday.get(Calendar.YEAR);
            int yearOfBirthday = calendarBirthday.get(Calendar.YEAR);
            if (yearOfToday - yearOfBirthday > 18) {
                return true;
            } else if (yearOfToday - yearOfBirthday == 18) {
                int monthOfToday = calendarToday.get(Calendar.MONTH);
                int monthOfBirthday = calendarBirthday.get(Calendar.MONTH);
                if (monthOfToday > monthOfBirthday) {
                    return true;
                } else if (monthOfToday == monthOfBirthday) {
                    if (calendarToday.get(Calendar.DAY_OF_MONTH) >= calendarBirthday.get(Calendar.DAY_OF_MONTH)) {
                        return true;
        } catch (ParseException e) {
        return false;
}, R.string.err_birth);

// to validate with your own custom validator function, warn and clear the warning with your way
mAwesomeValidation.addValidation(activity,, new CustomValidation() {
    public boolean compare(ValidationHolder validationHolder) {
        if (((Spinner) validationHolder.getView()).getSelectedItem().toString().equals("< Please select one >")) {
            return false;
        } else {
            return true;
}, new CustomValidationCallback() {
    public void execute(ValidationHolder validationHolder) {
        TextView textViewError = (TextView) ((Spinner) validationHolder.getView()).getSelectedView();
}, new CustomErrorReset() {
    public void reset(ValidationHolder validationHolder) {
        TextView textViewError = (TextView) ((Spinner) validationHolder.getView()).getSelectedView();
}, R.string.err_tech_stacks);

  • STEP 3
// Step 3: set a trigger
findViewById( View.OnClickListener() {
    public void onClick(View v) {
          // TODO perform your action

And you are done with your form validation.

NOTE – It doesn’t support UNDERLABEL style for ConstraintLayout

I hope this article will help you writing good validation code for your forms.

Happy Coding!!!

Leave a Reply

Your email address will not be published. Required fields are marked *