Implement Email Validator in Android
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.
Steps to Implement the Email Validator in Android
Step 1: Create an empty activity project
- Create an empty activity Android Studio project. Select Java as a programming language.
- To know how to create an empty activity Android Studio project refer to Android | How to Create/Start a New Project in Android Studio?.
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.
Step 3: Working with the MainActivity.java 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:
DOMAIN_NAME, IP_ADDRESS, PHONE, TOP_LEVEL_DOMAIN, WEB_URL.
- Refer to the Patterns for more information about the predefined patterns in Android.
- Invoke the following code inside the MainActivity.java file to implement the Email Validator in this case. Comments are added for a better understanding.