title: Validate Email is not working properly on Android
body: <p>I am trying to add a feedback form on my Android app where I need to test the email <code>textedit</code> field but it not work properly. Its always show me the message <code>"Please enter a valid email address"</code> id I put a valid address.</p>
  
  <pre><code>if (!isEmailValid(emailValid)){
      String estring = "Please enter a valid email address";
      @SuppressLint("ResourceAsColor") ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
      SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
      ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
      txtEmail.requestFocus();
      txtEmail.setError(ssbuilder);
  }
  
  
  public static boolean isEmailValid(String email) {
      String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
      Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
      Matcher matcher = pattern.matcher(email);
      return matcher.matches();
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/C7UYV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/C7UYV.png" alt="enter image description here"></a>
  <strong>Bellow is my full code.</strong></p>
  
  <pre><code>public class ContactFeedback extends AppCompatActivity {
      private Button mBtnFeedback;
      int ecolor = R.color.red;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.contact_form_ui);
  
          //layout fields
          final EditText txtName = findViewById(R.id.feedbackName);
          final EditText txtEmail = findViewById(R.id.feedbackEmail);
          final EditText txtMobile = findViewById(R.id.feedbackPhone);
          final EditText txtMessage = findViewById(R.id.feedbackMessage);
          final Button mBtnFeedback = findViewById(R.id.feedbackSubmit);
          final String emailValid = txtEmail.getText().toString().trim();
  
          mBtnFeedback.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  if(txtName.getText().toString().trim().equals(""))
                  {
                      Toast.makeText(getApplicationContext(), "Enter Your name", Toast.LENGTH_SHORT).show();
                      String estring = "Enter Your name";
                      @SuppressLint("ResourceAsColor") ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
                      SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
                      ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
                      txtName.requestFocus();
                      txtName.setError(ssbuilder);
                  }
                  else if(txtEmail.getText().toString().trim().equals(""))
                  {
                      Toast.makeText(getApplicationContext(), "Enter Your email", Toast.LENGTH_SHORT).show();
                      String estring = "Please enter a valid email address";
                      @SuppressLint("ResourceAsColor") ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
                      SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
                      ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
                      txtEmail.requestFocus();
                      txtEmail.setError(ssbuilder);
                  }
  
                  else if (!isEmailValid(emailValid)){
                      String estring = "Please enter a valid email address";
                      @SuppressLint("ResourceAsColor") ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
                      SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
                      ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
                      txtEmail.requestFocus();
                      txtEmail.setError(ssbuilder);
                  }
  
                  else if(txtMobile.getText().toString().trim().equals(""))
                  {
                      Toast.makeText(getApplicationContext(), "Enter Your Mobile", Toast.LENGTH_SHORT).show();
                      String estring = "Please enter a valid Mobile Number";
                      @SuppressLint("ResourceAsColor") ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
                      SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
                      ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
                      txtMobile.requestFocus();
                      txtMobile.setError(ssbuilder);
                  }
                  else if(txtMessage.getText().toString().trim().equals(""))
                  {
                      Toast.makeText(getApplicationContext(), "Enter Your Message", Toast.LENGTH_SHORT).show();
                      String estring = "Please enter Message";
                      @SuppressLint("ResourceAsColor") ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
                      SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
                      ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
                      txtMessage.requestFocus();
                      txtMessage.setError(ssbuilder);
                  }
  
                  else {
                      String name = txtName.getText().toString();
                      String email = txtEmail.getText().toString();
                      String message = txtMessage.getText().toString();
                      String phone = txtMobile.getText().toString();
  
                      String to = "example@myemail.com";
                      String subject = "Contact Feedback ";
  
                      Intent mEmail = new Intent(Intent.ACTION_SEND);
                      mEmail.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
                      mEmail.putExtra(Intent.EXTRA_SUBJECT, subject);
                      mEmail.putExtra(Intent.EXTRA_TEXT, "Name: " + name + "&lt;br /&gt;Mobile: " + phone + "&lt;br /&gt;Email: " + email + "&lt;br /&gt;Message: " + message);
  
                      // prompts to choose email client
                      mEmail.setType("message/rfc822");
  
                      startActivity(Intent.createChooser(mEmail, "Choose an email client to send your feedback!"));
                  }
  
              }
          });
  
  
      }
      public static boolean isEmailValid(String email) {
          String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
          Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
          Matcher matcher = pattern.matcher(email);
          return matcher.matches();
      }
  
  }
  </code></pre>