Validate Email in Android

Validating Email in Android is pretty simple and no need to write your own pattern matching code. What you have to decide is when you want to validate, there can be number of scenario but most common is:

  • On button press
  • On text Change
  • On focus change
In a example program we'll implement last two:-

Source Code

MainActivity.java (On button press)
package com.sonevalley.tech.emailvalidation;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView tvIsValidEmail;
    EditText edtEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvIsValidEmail = (TextView) findViewById(R.id.tvIsValidEmail);
        edtEmail = (EditText) findViewById(R.id.edtEmailId);
        edtEmail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                boolean flag = isValidEmail(edtEmail.getText().toString().trim());
                if (flag)
                    tvIsValidEmail.setText("Valid");
                else
                    tvIsValidEmail.setText("In-Valid");
            }
        });
    }

    public final static boolean isValidEmail(CharSequence target) {
        if (target == null) {
            return false;
        } else {
            return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
        }
    }
}

MainActivity.java (On focus change)
package com.sushil.tech.emailvalidation;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView tvIsValidEmail;
    EditText edtEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvIsValidEmail = (TextView) findViewById(R.id.tvIsValidEmail);
        edtEmail = (EditText) findViewById(R.id.edtEmailId);
        edtEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    boolean flag = isValidEmail(edtEmail.getText().toString().trim());
                    if (flag)
                        tvIsValidEmail.setText("Valid");
                    else
                        tvIsValidEmail.setText("In-Valid");
                }
            }
        });

    }

    public final static boolean isValidEmail(CharSequence target) {
        if (target == null) {
            return false;
        } else {
            return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
        }
    }
}

activity_main.xml (layout file)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edtEmailId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:hint="@string/email_id"/>

    <TextView
        android:id="@+id/tvIsValidEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/edtEmailId"
        android:layout_alignRight="@+id/edtEmailId"
        android:layout_alignEnd="@+id/edtEmailId"
        android:layout_marginRight="10dp"
        android:layout_marginEnd="10dp" />

</RelativeLayout>

Preview Screens:
Validate E-Mail in AndroidValidate E-Mail in Android



SHARE

0 comments:

Post a Comment