Skip to content Skip to sidebar Skip to footer

An Edittextpreference With Minimum Character Requirement

I have an EditTextPreference that I user to allow use to set a passcode to an app. I want to require a 4-digit passcode. I set the maxLength = '4' in the xml file. Now I have th

Solution 1:

private EditTextPreference preference;

this.preference = ((EditTextPreference) getPreferenceScreen() //put this in the onCreate                
                .findPreference("passcode"));

  if (key.equals("passcode")) {
        EditTextPreference setPasscode = (EditTextPreference) findPreference("passcode");
        if (sharedPreferences.getString("passcode","0").length() != 4) {
           Toast.makeText(this, "Should be 4 digits", Toast.LENGTH_LONG).show();
           this.preference.setText(null);
           return;
        }else{
              Toast.makeText(this, "Success!", Toast.LENGTH_LONG).show();
             }
    }

something like this should help you. Do keep in mind that getPreferenceScreen is deprecated, it is recommended to use PreferenceFragment. I am assuming that PreferenceActivity is being extended here.


Post a Comment for "An Edittextpreference With Minimum Character Requirement"