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 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:

compile '' //support for more design component
compile 'com.jakewharton:butterknife:8.8.1' //butter knife library


Now go to Activity  and write below code:

public class MainActivity extends AppCompatActivity {
    TextInputLayout tip_user;
    TextInputLayout tip_pass;
    EditText edt_pass;
    EditText edt_user;
    Button btn_login;
String user,pass;
    protected void onCreate(Bundle savedInstanceState) {
//      bind all resources to your activity
        // validation
        btn_login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
//             Nested if-else conditional statement for validation
                if (user.equals("")){
                    tip_user.setError("Please enter User Name");
                }else if (user.length()<6){
                    tip_user.setError("Please Enter Correct Username");
                }else if (pass.equals("")){
                    tip_pass.setError("Please Enter Password");
                }else if (pass.length()<4){
                    tip_pass.setError("Please Enter Correct Password");
//Write an intent to start a new Activity
//                    Intent intent=new Intent(MainActivity.this,abc.class);
//                    startActivity(intent);


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


  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

[amazon_link asins=’B078BNQ313,B077Q19RF9,B0756Z53JN,B077Q42DTC,B01MG7DBNW,B07DJL15QT,812653608X|B076CCY2D3,B079SQF4BD,B00O6RTF2W,B076M34B61,B0723B9XR6,B072PTK61S,B01J90OI4A|B079KGWRMZ,B0763XVMYD,B078KSJ7XV,B07FYN3DV1,B06XKJ7MN6,B076H3CVMB|B07G8DPZBH,B06XX31L73,B01MU9R2EN,2409010431,B01N4N3GVO,B01DNXSW2S,B07DD9L552′ template=’ProductCarousel’ store=’lifenews525-21|lifenews-21|lifenews0b-21|lifenews06-21′ marketplace=’IN|UK|IT|FR’ link_id=’ aaa266d5-d6b3-11e8-ac57-1919d491b3f4′]

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments

Enjoy this blog? Please spread the word :)