0

I have a method to validate a EditTextPreference. My method is executed after the confirmation of data by implementing the onSharedPreferenceChanged class.

However, only occurs after you confirm the information. I would perform the check without closing the dialog box. And if ok then close or keep open for user to enter the data correctly.

If it's not possible, I would reopen the dialog box if the validation is false.

SettingsActivity.java

class SettingsActivity extends PreferenceActivity implements
            OnSharedPreferenceChangeListener {

    protected static final String TAG = "SettingsActivity";
    private SharedPreferences prefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        // TODO Auto-generated method stub
        SharedPreferences.Editor prefEditor = prefs.edit();
        
        if(key.equals("pref_Url")){
            String url = prefs.getString(key, "");
            boolean response = (!new ConnectUtils().isConnected(this, url));
            
            if(!response){
                prefEditor.putString(key, Config.SERVER_URL_DEF_VALUE);
                prefEditor.commit();
                reload();
                Toast.makeText(this,R.string.msgToast_server_url_invalid,Toast.LENGTH_SHORT).show();
            }
            
        }else if (key.equals("pref_Id")){
            String url = Config.SERVER_URL_ID;
            boolean reponse = (!new ConnectUtils().isConnected(this,url));
            
            if(!reponse){
                prefEditor.putString(key,Config.ID_DEF_VALUE);
                prefEditor.commit();
                reload();
                Toast.makeText(Config.getContext(), R.string.msgToast_Id_invalid, Toast.LENGTH_SHORT).show();               
            }           
        }
    }   
    private void reload(){
        startActivity(getIntent()); 
        finish();       
    }
}
Community
  • 1
  • 1

1 Answers1

0

'onPreferenceChangeListener' is a listener that is executed every time a preference is changed by the user. You can return true if data complains validation or false otherwise.

For example:

 public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    findPreference("pre_mail").setOnPreferenceChangeListener(
        new Preference.OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            return Pattern.matches(Constants.MAILPATTERN, (String) newValue);
        }

    });
}

}

Hope this help!

pozuelog
  • 1,284
  • 13
  • 27
  • I don't know if this help. Because my validation execute in a AsyncTask. I want to the is call when user press ok. This method is called whenever a character is inserted ? – Herculano Gripp Jan 15 '14 at 17:39