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. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="Email_Id"
  14. android:id="@+id/textView"
  15. android:layout_marginLeft="13dp"
  16. android:layout_marginTop="27dp"
  17. android:layout_alignParentTop="true"
  18. android:layout_alignParentLeft="true"/>
  19. <EditText
  20. android:id="@+id/emailEditText"
  21. android:layout_width="250dp"
  22. android:layout_height="wrap_content"
  23. android:ems="10"
  24. android:layout_alignTop="@+id/textView"
  25. android:layout_alignParentRight="true"
  26. />
  27. <Button
  28. android:id="@+id/sendButton"
  29. android:layout_height="wrap_content"
  30. android:layout_width="wrap_content"
  31. android:text="SEND"
  32. android:layout_marginTop="38dp" android:layout_below="@+id/emailEditText"
  33. android:layout_centerHorizontal="true"/>
  34. <TextView
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:textAppearance="?android:attr/textAppearanceLarge"
  38. android:text=""
  39. android:id="@+id/textViewmessage"
  40. android:layout_marginTop="32dp"
  41. android:layout_below="@+id/sendButton"
  42. android:layout_centerHorizontal="true"/>
  43. </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. public class MainActivity extends Activity {
  10. Button emailButton;
  11. EditText emailEdittext;
  12. TextView textviewMessage;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. emailButton=(Button)findViewById(R.id.sendButton);
  18. emailEdittext=(EditText)findViewById(R.id.emailEditText);
  19. textviewMessage=(TextView)findViewById(R.id.textViewmessage);
  20. emailButton.setOnClickListener(new View.OnClickListener() {
  21. @Override
  22. public void onClick(View view) {
  23. String getText=emailEdittext.getText().toString();
  24. String Expn =
  25. "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
  26. +"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
  27. +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
  28. +"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
  29. +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
  30. +"([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";
  31. if (getText.matches(Expn) && getText.length() > 0)
  32. {
  33. textviewMessage.setText("valid email");
  34. }
  35. else
  36. {
  37. textviewMessage.setText("invalid email");
  38. }
  39. }
  40. public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
  41. public void onTextChanged(CharSequence s, int start, int before, int count) {}
  42. });
  43. }
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