0

I have a preference screen that, when a setting is enabled, has several strings that must be a particular length (# of characters). Also all of the strings are required. Ideally when the user has enabled this setting they should not be able to apply the changes until all strings are filed in with the appropriate length. Note that I'm not talking about a max length, but an exact character count.

How can I enforce those rules on the user? Is there a way to do this through the built-in preference controls or do I have to build a custom fragment?

Travis Christian
  • 2,412
  • 2
  • 24
  • 41
  • 2
    shee here: http://stackoverflow.com/questions/2535132/how-do-you-validate-the-format-and-values-of-edittextpreference-entered-in-andro – FoamyGuy Apr 05 '13 at 20:21

1 Answers1

1

The Preference class has the onPreferenceChange listener. public abstract boolean onPreferenceChange (Preference preference, Object newValue);

By returning false from this listener, the new value will not be accepted:

public boolean onPreferenceChange(Preference pref, Object newValue) {
  if (pref == myStringPreferenceToValidate) {
      final String newValueStr = (String) newValue;
      if (newValueStr.length() != XX) {
        // don't save it, not the correct length
        return false;
      }
      return true;
   }
}

Check out the full documentation here: http://developer.android.com/reference/android/preference/Preference.OnPreferenceChangeListener.html

al.
  • 212
  • 1
  • 2
  • 6