Android Form Validation Tutorial using AwesomeValidation Library

Today we will learn about Android Form Validation. In any app input validation is a very important thing to do. Input Validation eliminates the errors that can be done by user while giving inputs to our app. For example if we want to get the user’s email we can check the entered email is a valid email or not before storing it inside the database. So lets start our Android Form Validation tutorial.

Creating a New Project

  • The first step as always to create a new project.
  • So create a new Android Studio Project and come inside your activity_main.xml to design our form.

Creating a Form

  • Now to validate we need a form. So inside activity_main.xml create some input fields. I have designed the screen given below.
Android Form Validation
Android Form Validation
  • In the above screen we have 5 EditTexts and 1 Button. Now we will validate these fields for the connect input when the button is pressed. If you are facing trouble creating the EditTexts, you can copy the following xml code.

Android Form Validation

Adding AwesomeValidation Library

  • So we are going to use AwesomeValidation. So first come inside app level build.gradle file and add the following line inside dependencies block.

  • Now after adding the above line sync your project.

Defining View Objects

  • Now first we need to define all the View Objects that is needed. So in this case we will define the objects inside

Defining Strings for Error Messages

  • To display the error messages we need to define all the strings needed inside strings.xml file. So define the following strings inside strings.xml.

Validating Input Fields

  • To validate the input the modify the code as below.

What we did?

  • First we defined an AwesomeValidation object.
  • We initialized the object with BASIC style, we have two more different style COLORATION and UNDERLABEL.
  • Then we added the validation to required EditTexts using addValidation() method with AwesomeValidation object.
    addValidation() : This method takes 4 arguments.

    1. Activity Context, we used this to pass the current activity context.
    2. Resource id of EditText in which we want to add validation, we used
    3. Regex String for adding validation, you can use Regex for more kind of validations
    4. String resource id for error message to show the error if validation fails
  • Then in submitForm() method we are validating the inputs with validate() method. The method returns true or false, true if validation succeeds and false if validation fails.

Testing the App

  • Now you can run the app to test whether it is working or not.

android form validation example

  • You can see in the above screenshot it is working absolutely fine.

So thats all for this Android Form Validation Tutorial friends. Please share this post if you found it useful. And also don’t hesitate to leave your comments if you are facing any troubles. Thank You 🙂

15 thoughts on “Android Form Validation Tutorial using AwesomeValidation Library”

  1. Cool, but in your activity_main.xml file, you gave inputType =”textEmailAddress” to the mobile number field. Is it a typo or deliberate?

  2. Hello Mr Belal,
    Thank you for all you have been posting. i wish you more success in your endavour. Please i want to ask if you could post an article on chat messages for android using smack/xmmp with openfire server to send and receive chat messages. Thanks very much in advance.

  3. Hello bilal, I’m new in android. I want ask one question that, How much reliable Awesome Validation. Can i get support in future if implemented in my application ?

  4. Hello sir,

    Thank you for posting this tutorial! It really helped me with the creation of my app and I learned again! Much appreciated! Just wanted to let you know that in the awesomeValidation.addValidation you use for email, mobile and the dob the same “R.string.nameerror”.

    Thanks again! Br Ismail


Leave a Comment