Open In App
Related Articles

Implement Email Validator in Android

Like Article
Save Article
Report issue

Prerequisite: EditText widget in Android using Java with Examples

Email validation is required to inform the user that the Email entered by the user is not in a valid format. If the user has not entered the Email in the wrong format then the error filed is to be given for the EditText. In this article, it’s been discussed how to implement the Email validator in Android step by step. Have a look at the following image to get an idea of what’s been discussed in this article. Note that we are going to implement this project using the Java language. 

Implement Email Validator in Android

Steps to Implement the Email Validator in Android

Step 1: Create an empty activity project

Step 2: Working with the activity_main.xml file

  • In the main layout of the application, only two of the widgets are implemented. One is the Email EditText field and one Button which when clicked the entered email is to be checked.
  • Invoke the following code in the activity_main.xml file to implement the UI layout.


<?xml version="1.0" encoding="utf-8"?>
    <!--EditText which takes input
        as Email from the user-->
        android:hint="Email" />
    <!--Button which when clicked validates
        the entered email is valid or not-->
        android:textColor="@android:color/white" />


Output UI:

Implement Email Validator in Android

Step 3: Working with the file

  • In this case, the pattern EMAIL_ADDRESS is used for demonstration purposes.
  • However, there are 5 more patterns to get validate the input from the user. Those are:


  • Refer to the Patterns for more information about the predefined patterns in Android.
  • Invoke the following code inside the file to implement the Email Validator in this case. Comments are added for a better understanding.


import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    // EditText filed for Email
    EditText etMail;
    // Button to validate the Email address
    Button bValidate;
    protected void onCreate(Bundle savedInstanceState) {
        // register all the UI elements
          // with their appropriate IDs
        etMail = findViewById(;
        bValidate = findViewById(;
        // handle the VALIDATE button to show the toast
          // message whether the entered email is valid or not
        bValidate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
    // the function which triggered when the VALIDATE button is clicked
    // which validates the email address entered by the user
    public void emailValidator(EditText etMail) {
        // extract the entered data from the EditText
        String emailToText = etMail.getText().toString();
        // Android offers the inbuilt patterns which the entered
          // data from the EditText field needs to be compared with
        // In this case the entered data needs to compared with
          // the EMAIL_ADDRESS, which is implemented same below
        if (!emailToText.isEmpty() && Patterns.EMAIL_ADDRESS.matcher(emailToText).matches()) {
            Toast.makeText(this, "Email Verified !", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Enter valid Email address !", Toast.LENGTH_SHORT).show();



// Kotlin code for email validator
package com.example.gfgemailvalidator
import android.os.Bundle
import android.util.Patterns
import android.view.View
import android.widget.EditText
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    private lateinit var myeditText: EditText
    private lateinit var email: String
    override fun onCreate(savedInstanceState: Bundle?) {
         // registering IDs for views
        myeditText = findViewById(
        email = myeditText.text.toString().trim()
    fun check(view: View) {
        // Getting the user input
        var text1 = myeditText?.text
        if(text1.isEmpty()) // check if user have not entered then ask for enter
            Toast.makeText(this@MainActivity,"enter your email id",Toast.LENGTH_LONG).show()
            if (Patterns.EMAIL_ADDRESS.matcher(text1).matches()) { // using EMAIL_ADREES matcher
                Toast.makeText(this@MainActivity, "MATCH", Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(this@MainActivity, "NO MATCH", Toast.LENGTH_LONG).show()


Output: Run on Emulator

Last Updated : 19 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads