How do you validate the format and values of EditTextPreference entered in Android 2.1?

10,549

Solution 1

Your question was an early google hit when I was trying to do the same thing, so hopefully this helps someone. Here's something I hacked together today that demonstrates OnPreferenceChangeListener, thus allowing you to stop invalid changes.

in your fragment:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        Your_Pref = (EditTextPreference) getPreferenceScreen().findPreference("Your_Pref");

        Your_Pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                Boolean rtnval = true;
                if (Your_Test) {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setTitle("Invalid Input");
                    builder.setMessage("Something's gone wrong...");
                    builder.setPositiveButton(android.R.string.ok, null);
                    builder.show();
                    rtnval = false;
                }
                return rtnval;
            }
        });
    }

Solution 2

Implement Preference.OnPreferenceChangeListener

boolean onPreferenceChange(Preference preference, Object newValue)

Called when a Preference has been changed by the user. This is called before the state of the Preference is about to be updated and before the state is persisted.

Returns True to update the state of the Preference with the new value.

So you can directly return the result of value validation.

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        findPreference("mail_preference_key").setOnPreferenceChangeListener(
            new Preference.OnPreferenceChangeListener() {

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

        });
    }
}

Solution 3

I'd use Preference.OnPreferenceChangeListener rather than SharedPreferences.OnSharedPreferenceChangeListener.

The former allows you to validate the new value before it's persisted (and prevent it from being persisted) rather than after.

Share:
10,549
Bryan C
Author by

Bryan C

Updated on June 05, 2022

Comments

  • Bryan C
    Bryan C about 2 years

    Does anyone have sample code to validate user entered text in preferences? For example, I have a EditTextPreference for user to enter an email address. I'd like to validate the format of email address entered and pop up an alert dialog if the format isn't correct. Anyone have any sample code for this? Thanks

  • sargas
    sargas over 13 years
    I have the following code: if(key.equals("update_delay")) { String newvalue = sp.getString(key, null); if(Integer.parseInt(newvalue) < 60) sp.edit().putString("update_delay", "60").commit(); } and the next time I click on the EditTextPreference it still shows the low value. Do you know if commit() doesn't work in this context?
  • Fede
    Fede over 13 years
    I'm not sure, but in commit method reference says 1. Note that when two editors are modifying preferences at the same time, the last one to call commit wins. 2. Returns true if the new values were successfully written to persistent storage. Point 1: Maybe the user editor make the last call. Point 2: Check if commit return true. Hope this help.
  • iozee
    iozee almost 12 years
    Isn't this called after the change is done? I suppose @Aaron has the right answer.