Validation In Android

As a Beginner in Android, everyone looks for ways to apply “validations”.  there are different ways to implement validations but for me, this way works! if you find it easier too then feel free to mention in the comment section below.

Why Validations?

Validations can make users be restricted to login/signup to your application without proper credentials.

For Example:

  1. You only want your users to have GMAIL account, other email accounts like yahoo, outlook, Hotmail cannot login/signup in your application. Then you need proper validation for that if a user enters email address other than @gmail.com than it will show an error. For email validation, you must use Regex, it is easier to use regex for matching the pattern of the text user entered and the pattern you want by default.
  2. Similarly, you want your password to be only 6 character length, then you have to apply proper validation on that field.

Now, What are different ways to apply validation?

  1. Directly insert all the validation if-else statement in the code. As I did.
  2. Make another class called “validation” and create a function for validating your field. This is the best approach anyone would recommend but as we are just beginning we will use easy way at the start. After you have created a class and function you can call that function in your activity via class object. We will go to this approach Later.

Let’s start with validation of a simple Login Screen. In this tutorial Our validations Will be:

  1. For User Field: Write name more than 6 characters.
  2. For Password field: It should be More than Four Characters.

*I am showing you the simplest example of validation/ Subscribe to get an update on a new post for learning Android for Free.

 

If you have not created a Login Page yet, Create an application a simple login Page by yourself or find code here!

Go to Your App Gradle file and add butterknife library  and design library:

 

Now go to Activity  and write below code:

 

Thank You and don’t forget to share this with your beginner android developer friend!

Tips

  1. Using Butterknife Library is much easier than writing findviewbyid() every time.
  2. There are other ways for validations where you make a different class called validation and call the function in your Main Activity.
  3. Always complete one if-else statement before starting another to avoid confusions.
  4. I recommend using curly braces after each if-else doesn’t matter you are writing only one statement in it or more. It will be easier for you to understand the validation code in case of a Bug.
  5. Don’t forget to add, in your onCreate Method, when using Butterknife

How useful was this post?

Do not forget to rate! this helps us to Improve.

Average rating / 5. Vote count:

Read More Like This..

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Safiya Akhtar

Safiya Akhtar is Android Application Developer in India. She is currently working as a public consultant for career and education too, besides blogging and freelancing. She believes that it is never too late or too early to start what you have always dreamed of.

Your Views And Question are Welcome Here!