how to check email address is valid or not in android example.
In this article today learn how to check email address is
valid or not in android example. we will see different methods to validate
email address entered through EditText using android layout xml file. Email
validation is useful in android because we need to send the email to server for
registering a new account or for any other purpose.
Follow the method how
to check email address is valid or not in android:
Declaire a variable:
String emailPattern =
"[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
Then the button click
event part declaired:
button.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
if(emailId.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(),"enter email
address",Toast.LENGTH_SHORT).show();
}else {
if
(emailId.getText().toString().trim().matches(emailPattern)) {
Toast.makeText(getApplicationContext(),"valid
email address",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),"Invalid email
address", Toast.LENGTH_SHORT).show();
}
}
}
});
Follow the full source code how to check email address is valid or not in android:
1.MainActivity.java:
package com.akash.emailvalidaton;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int view = R.layout.activity_main;
Button button;
EditText emailId;
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.text);
emailId = findViewById(R.id.emailId);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(emailId.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(),"enter email address",Toast.LENGTH_SHORT).show();
}else {
if (emailId.getText().toString().trim().matches(emailPattern)) {
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),"Invalid email address", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
2.activity_main.xml:
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:id = "@+id/parent"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity"
android:gravity = "center"
android:orientation = "vertical">
<EditText
android:id = "@+id/emailId"
android:hint = "Enter Email id"
android:layout_margin = "20dp"
android:layout_width = "match_parent"
android:layout_height = "wrap_content" />
<Button
android:id = "@+id/text"
android:textSize = "18sp"
android:textAlignment = "center"
android:layout_width = "wrap_content"
android:textColor = "#000"
android:text = "Check validation"
android:layout_height = "wrap_content" />
</LinearLayout>
3.AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.akash.emailvalidaton">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
4.string.xml:
<resources>
<string name="app_name">email validaton</string>
</resources>
5.style.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
6.Output:
No comments:
Post a Comment