How to Validate Email in Android Studio

Introduction

 
In this tutorial you will learn how to validate Email_id in Android Studio. To validate Email_id we must use the regex code (regular expression).
 
regular expression:-
 
"^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
                                +"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
                                +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
                                +"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
                                +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                                +"([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";
 
Step 1
 
Create a new project:
 
a.jpg
 
Step 2
 
Open your XML file and use an element like this:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity">  
  10.    
  11.       <TextView  
  12.             android:layout_width="wrap_content"  
  13.             android:layout_height="wrap_content"  
  14.             android:text="Email_Id"  
  15.             android:id="@+id/textView"  
  16.             android:layout_marginLeft="13dp"  
  17.             android:layout_marginTop="27dp"  
  18.             android:layout_alignParentTop="true"  
  19.             android:layout_alignParentLeft="true"/>  
  20.        
  21.       <EditText  
  22.         android:id="@+id/emailEditText"  
  23.         android:layout_width="250dp"  
  24.         android:layout_height="wrap_content"  
  25.         android:ems="10"  
  26.         android:layout_alignTop="@+id/textView"  
  27.         android:layout_alignParentRight="true"  
  28.       />  
  29.     
  30.      <Button  
  31.         android:id="@+id/sendButton"  
  32.         android:layout_height="wrap_content"  
  33.         android:layout_width="wrap_content"  
  34.         android:text="SEND"  
  35.         android:layout_marginTop="38dp" android:layout_below="@+id/emailEditText"  
  36.         android:layout_centerHorizontal="true"/>  
  37.      
  38.      <TextView  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:textAppearance="?android:attr/textAppearanceLarge"  
  42.             android:text=""  
  43.             android:id="@+id/textViewmessage"  
  44.             android:layout_marginTop="32dp"  
  45.             android:layout_below="@+id/sendButton"  
  46.             android:layout_centerHorizontal="true"/>  
  47.    
  48.  </RelativeLayout> 
Step 3
 
Open the Java file and write this:
  1. package com.emailvalidation;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8. import android.widget.TextView;  
  9.    
  10. public class MainActivity extends Activity {  
  11. Button emailButton;  
  12. EditText emailEdittext;  
  13. TextView textviewMessage;  
  14.    
  15.         @Override  
  16.         protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         emailButton=(Button)findViewById(R.id.sendButton);  
  20.         emailEdittext=(EditText)findViewById(R.id.emailEditText);  
  21.         textviewMessage=(TextView)findViewById(R.id.textViewmessage);  
  22.    
  23.         emailButton.setOnClickListener(new View.OnClickListener() {  
  24.             @Override  
  25.             public void onClick(View view) {  
  26.                 String getText=emailEdittext.getText().toString();  
  27.                 String Expn =  
  28.                         "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"  
  29.                                 +"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"  
  30.                                 +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."  
  31.                                 +"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"  
  32.                                 +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"  
  33.                                 +"([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";  
  34.    
  35.                         if (getText.matches(Expn) && getText.length() > 0)  
  36.                         {  
  37.                             textviewMessage.setText("valid email");  
  38.                         }  
  39.                         else  
  40.                         {  
  41.                             textviewMessage.setText("invalid email");  
  42.                         }  
  43.                     }  
  44.             public void beforeTextChanged(CharSequence s, int start, int count, int after) {}  
  45.             public void onTextChanged(CharSequence s, int start, int before, int count) {}  
  46.                 });  
  47.             } 
Step 4
 
See the output.
 
When we enter an invalid Email_id:
 
e.jpg
 
Step 5
 
When we enter a valid Email_id:
 
d.jpg


Similar Articles