How can I verify a user enters email address in a EditTextPreference for android setting ?
            Asked
            
        
        
            Active
            
        
            Viewed 753 times
        
    0
            
            
        - 
                    refer http://stackoverflow.com/questions/9355899/android-email-edittext-validation – sasikumar Dec 28 '16 at 08:33
- 
                    Possible duplicate of [Android Email Validation on EditText](http://stackoverflow.com/questions/24969894/android-email-validation-on-edittext) – Patel Pinkal Dec 28 '16 at 08:38
- 
                    I think what the author means is to validate the email entered in `EditTextPreference` before saving it to preference – Glenn Dec 28 '16 at 08:41
- 
                    2Possible duplicate of [How should I validate an e-mail address?](http://stackoverflow.com/questions/1819142/how-should-i-validate-an-e-mail-address) – Charuක Dec 28 '16 at 08:42
3 Answers
3
            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();
 
    
    
        Sadiq Md Asif
        
- 882
- 6
- 18
2
            
            
        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.
 
    
    
        fardown
        
- 713
- 2
- 12
- 23
0
            
            
        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().
 
    
    
        Monish Kamble
        
- 1,478
- 1
- 15
- 28
 
    