Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating email does not work with textInputLayout

I have the textInputLayout for which i added the listener. I'm trying to check if the enter email is valid or not. To do this i have written the below code. but it does not work. Even if i give the right email id it shows valid email id.

package com.hari.ga.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hari.ga.lending.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Shiva on 17-10-2015.
 */
public class Personal extends AppCompatActivity {

    protected Button next;
    EditText editText;
    String e;
    Boolean check;
    protected TextInputLayout emailText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_details);
        editText = (EditText)findViewById(R.id.email);
        next = (Button)findViewById(R.id.next_button);
       emailText = (TextInputLayout)findViewById(R.id.emailText);
       e = editText.getText().toString();


        emailText.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                boolean c = checkEmail(e);
                if (!c) {
                    emailText.setError("valid email id is required");
                    emailText.setErrorEnabled(true);
                } else {
                  emailText.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
      /*  editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(e)) {
                    Snackbar.make(v, e, Snackbar.LENGTH_SHORT).show();
                    emailText.setErrorEnabled(false);
                } else {
                    emailText.setError("Input required");
                    emailText.setErrorEnabled(true);
                }
            }
        }); */
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(check==false) {
                    Toast.makeText(getApplicationContext(), "Please make sure the details are right", Toast.LENGTH_LONG).show();

                } else{
                    Intent intent = new Intent(Personal.this, HomeDetails.class);
                    startActivity(intent);
                }


            }
        });
    }
    public static boolean checkEmail(String email) {

        Pattern EMAIL_ADDRESS_PATTERN = Pattern
                .compile("[a-zA-Z0-9+._%-+]{1,256}" + "@"
                        + "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" + "(" + "."
                        + "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" + ")+");
        return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
    }
}
like image 672
Vivek_Neel Avatar asked Sep 12 '25 00:09

Vivek_Neel


1 Answers

This should work,

android.util.Patterns.EMAIL_ADDRESS.matcher(emailText.getText().toString()).matches())
like image 110
Madhur Avatar answered Sep 13 '25 14:09

Madhur