Validating any data before submitting it to the server has always been a tricky but easier task when you are using a proper library. If you are trying to achieve the same goal without using any library, it would be an extra headache to cover all the test cases. Specially when you have many fields in your page. So, using library is always a better choice. There are so many libraries in the market that can be used for validation. For example, Android Saripaar library for Validation, Android AwesomeValidation library for validation etc. I have already covered Android Saripaar library. If you want, you can go through this post. In this post, I am going to discuss about Android AwesomeValidation library. Like Android Saripaar library, Android AwesomeValidation Library is also easy to setup and use in the android application. You might be thinking which options are better? which library should i use? etc. You will get answer to these questions in this post.
Android AwesomeValidation library supports following validation rules. String Type Numeric AlphaNumeric IP Address Web Url Phone Number Year
In this tutorial, you will learn how to use Android AwesomeValidation library to validate any form before submitting it to the server.
1. Creating New Project
Follow steps written below to create a new project.
a. Goto File => New => New Project. Then, Write application name as FormValidationAwesomeLibrary and click next.
b. Select Minimum SDK 17 or more => click next => Select Empty Activity => click next => click finish.
If you have followed above process correctly, you will get a newly created project successfully. If you want to know how to create a project in detail, you can go to creating first project
1.1 Basic setup to configure Android AwesomeValidation library
Now, we will do some basic setup to configure the project to use Android AwesomeValidation library. Follow steps below –
1.1.1 Add gradle into app/build.gradle file
Add gradle for Android AwesomeValidation library into app/build.gradle file.
compile 'com.basgeekball:awesome-validation:2.0'
Finally, your app/build.gradle file would look like below.
<string name="terms_and_conditions">I agree to the terms and conditions</string>
<string name="invalid_name">Please enter a valid Name</string>
<string name="invalid_email">Please enter a valid email</string>
<string name="invalid_confirm_email">Email does not match</string>
<string name="invalid_password">Please enter a valid password</string>
<string name="invalid_confirm_password">Password does not match</string>
<string name="invalid_phone">Please enter a valid Phone</string>
<string name="invalid_age">Age must be between 12and 60.</string>
</resources>
There are no changes in colors.xml and styles.xml files, so i am not going to add these here. If you need reference, you can download the sourcecode.
1.2.2 Creating sign up form to use AwesomeValidation library
Everything is ready now. So, let’s create the UI. In this tutorial, we are going to see how to validate a sample sign up form using AwesomeValidation library. This sample signup form will contain name, email, confirm email, password, confirm password, phone and age.
Now, open res ==> layout ==> activity_main.xml file and copy/paste below code to create sign up form that contains these fields. res ==> layout ==> activity_main.xml file code
Now, the next task is accessing these fields in java file. So, i am going to write the code to show how you can access these fields. Open MainActivity.java file and write below code into it.
Now, we are ready to use AwesomeValidation library. We will see implementation of AwesomeValidation library in three steps. 1. Declaring validation style 2. Add required validations 3. Set a point when you want to trigger validation
1.3.1 Declaring Validation Style
To implement validation in the form, you will need to create an instance of AwesomeValidation. While creating validation style, you need to take care of the cases where you are going to implement this library. AwesomeValidation library supports following style type
– BASIC
– COLORATION
– UNDERLABEL
– TEXT_INPUT_LAYOUT
Let’s get some ideas about these types
A. BASIC Style Type: In this style type, you will get error message below the View (EditText, Spinner etc.) Also, You will see the message one by one. If you want to use this type of validation style, you can declare validation object as below
B. COLORATION Style Type: In this style type, Incorrect input will be highlighted. You can set the color as well to show the incorrect input. However, the default color is red. If you want to use this type of validation style, you can declare validation object as below
mAwesomeValidation.setColor(Color.YELLOW); // optional, default color is RED if not set
C. UNDERLABEL Style Type: In this style type, you will get error message below the View all at a time. This is difference between BASIC and UNDERLABEL style type. If you want to use this type of validation, you can declare validation object as below.
mAwesomeValidation.setContext(this); // mandatory for UNDERLABEL style
D. TEXT_INPUT_LAYOUT Style Type: This style is used with text_input_layout to show error message in Within text_input_layout. If you want to use this type of validation object, you can declare validation object as below.
The next step is to add the validation we want to be validated. We are just showing an example how will you add the validation. You can add other validation similarly.
To add any validation, you just need to call addValidation method. For example,
Here, we have called the addValidation method and passed context, id of the view in which validation is applied, regex to validate and the message to show if validation is unsuccessful.
Similarly, you can add other validation as well. I have covered different types of validation you may need. For example, I have shown how to validation email, confirm email field, password, confirm password field etc.
Most of the hectic work is done now. The next step is to trigger the validation to validate the fields which you want to validate. To validate, you just need to call awesomeValidation.validate(). If there is some error in validation, you will get error message according to the type of style you have use. i.e. if you have used BAISC style, error message will be shown one by one. If you have used COLORATION style, incorrect inputs will be highlighted.
It’s better to trigger the validation when you want to submit the form. On click of Sumit button, you call awesomeValidation.validate(). This way you can validate the form just before submitting it to the server.
1.4. Final MainActivity.java code
After applying validation, finally our MainActivity class would look like below. MainActivity.java
If you run the application, you will get output as given above. If you have any questions or got some problems while following this tutorial, you can download the source code as well. Source code link is already given above.
Conclusion
Android AwesomeValidation library is an easy to configure and use for Form Validation in Android. You just need to add some few lines to code and you are done. You will get output as desired. However, There is also another popular library(Saripaar Library) that can be used for form validation. You can use either one as per your need. Both libraries have some advantages and dis-advantages. So, which one to use depends on your need.
awesomeValidation.addValidation(this, R.id.etCpassword, R.id.etPassword, R.string.confirmpassworderror);
I am getting error to this line that “cannot resolve method….”
???
At my end, i am not getting this error. Please verify following things
1. Which version of awesomeValidation you are using? it should be same as what i am using in this tutorial.
2. Did you press Sync after adding gradle?
3. Are you using it in Activity? I have used “this” because the method is being used in activity.
Note: You may download source code from given link.
awesomeValidation.addValidation(this, R.id.etCpassword, R.id.etPassword, R.string.confirmpassworderror);
I am getting error to this line that “cannot resolve method….”
???
At my end, i am not getting this error. Please verify following things
1. Which version of awesomeValidation you are using? it should be same as what i am using in this tutorial.
2. Did you press Sync after adding gradle?
3. Are you using it in Activity? I have used “this” because the method is being used in activity.
Note: You may download source code from given link.