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 …

android form validation tutorial

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 MainActivity.java.

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 R.id.viewid
    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 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x