Android
Email Validation
EditText
Mobile Development
Duplicate Question

Email Address Validation in Android on EditText

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

When you validate email input on Android, the goal is usually to catch obvious formatting mistakes before the user submits a form. EditText makes that easy, but the best solution is to combine input hints, lightweight local validation, and clear error feedback rather than relying on a giant custom regular expression.

The platform already gives you useful tools for this. In most cases, Patterns.EMAIL_ADDRESS plus a trimmed EditText value is the right starting point.

Start With the Right Input Type

Even before validation, configure the field so the keyboard and autofill behavior match the expected input:

xml
1<com.google.android.material.textfield.TextInputLayout
2    android:id="@+id/emailLayout"
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content">
5
6    <com.google.android.material.textfield.TextInputEditText
7        android:id="@+id/emailEditText"
8        android:layout_width="match_parent"
9        android:layout_height="wrap_content"
10        android:hint="Email"
11        android:inputType="textEmailAddress" />
12
13</com.google.android.material.textfield.TextInputLayout>

textEmailAddress does not validate the value by itself, but it improves the typing experience and reduces obvious entry mistakes.

Validate With Patterns.EMAIL_ADDRESS

For local format checks, Android's built-in email pattern is usually enough:

kotlin
1import android.util.Patterns
2
3fun isValidEmail(value: CharSequence?): Boolean {
4    val text = value?.toString()?.trim().orEmpty()
5    return text.isNotEmpty() && Patterns.EMAIL_ADDRESS.matcher(text).matches()
6}

You can call that method when the user taps a submit button:

kotlin
1binding.submitButton.setOnClickListener {
2    val email = binding.emailEditText.text
3
4    if (isValidEmail(email)) {
5        binding.emailLayout.error = null
6        submitForm()
7    } else {
8        binding.emailLayout.error = "Enter a valid email address"
9    }
10}

This keeps validation logic centralized and avoids sprinkling regex checks around the screen.

Show Feedback While the User Types

If you want earlier feedback, use doAfterTextChanged from androidx.core.widget:

kotlin
1import androidx.core.widget.doAfterTextChanged
2
3binding.emailEditText.doAfterTextChanged { editable ->
4    binding.emailLayout.error =
5        if (editable.isNullOrBlank() || isValidEmail(editable)) {
6            null
7        } else {
8            "Enter a valid email address"
9        }
10}

This pattern is friendlier than rejecting every intermediate keystroke. The user can type naturally, and the field only shows an error when the current text clearly does not look like an email address.

Understand What Format Validation Can and Cannot Do

A valid-looking email string is not the same thing as a real, deliverable email address. Local validation can tell you whether the text matches a reasonable email shape. It cannot prove that:

  • the domain exists
  • the mailbox is active
  • the address belongs to the current user

For that, you need server-side verification and usually a confirmation email flow. Client-side validation is still useful because it catches obvious mistakes early and improves the user experience.

Keep the Validation Rule Practical

Trying to implement the complete email specification in one regex is usually not worth it on Android forms. Real-world apps benefit more from a practical rule that catches common mistakes than from a theoretically perfect parser that is hard to maintain.

In most app screens, the combination below is enough:

  • trim the text
  • reject blank input
  • use Patterns.EMAIL_ADDRESS
  • verify ownership later on the backend

That approach is easier to read, easier to test, and less likely to reject valid addresses unnecessarily.

Common Pitfalls

  • Using a custom regex that is stricter than the addresses your users actually have.
  • Forgetting to trim the EditText value, which causes valid addresses with accidental spaces to fail.
  • Showing an error too aggressively while the user is still typing the first few characters.
  • Assuming a valid format means the address exists or belongs to the user.
  • Relying only on client-side validation. Important checks still belong on the server.

Summary

  • Set android:inputType="textEmailAddress" so the field behaves like an email input from the start.
  • Use Patterns.EMAIL_ADDRESS for practical client-side format validation.
  • Trim the EditText value before checking it.
  • Show errors through TextInputLayout on submit or after text changes for better UX.
  • Treat format validation as a first pass only; real verification happens on the server.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.