How to verify email input in android settings

756 views Asked by At

How can I verify a user enters email address in a EditTextPreference for android setting ?

3

There are 3 answers

0
Sadiq Md Asif On BEST ANSWER

By the way, You can use inputType in EditText to validate the input as email by the system!

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:id="@+id/editText" />

You can use the default utility to validate!

 boolean isEmailValid(CharSequence email) {
            return android.util.Patterns.EMAIL_ADDRESS.matcher(email)
                    .matches();
0
Monish Kamble On

You must read it from SharedPreference like this.

String email = PreferenceManager.getDefaultSharedPreferences(context).getString("your_pref_name","default_value")

if(!TextUtils.isEmpty(email)) {
    //Perform operation on Email
} else {
   //Show error
}

Listening to Preference Changes

If you want to listen to Preference changes, you must register a OnPreferenceChangeListener on your Preference and handle the inputs inside onPreferenceChange().

0
fardown On

Import org.apache.commons.validator.routines.EmailValidator as one of your dependencies then use this method EmailValidator.getInstance().isValid("editText_email").

It does the necessary RegEx checks to validate if the email is valid or not.