0

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 the problem to not allow submit unless the entered passcode is 4 digits long.

Here is what I have:

 <EditTextPreference
            android:defaultValue=""
            android:dependency="EnablePasscode"
            android:dialogMessage="Add a numeric passcode to provide access to enter the app.  The passcode must be 4 digits."
            android:dialogTitle="Set Passcode"
            android:inputType="number"
            android:key="passcode"
            android:maxLength="4"
            android:password="true"
            android:title="Set Passcode" />

Now in Java:

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {

    if (key.equals("passcode")) {
        EditTextPreference setPasscode = (EditTextPreference) findPreference("passcode");
        if (setPasscode.getText().toString().length() == 4) {
            // return true
        }
    }

}

Where it says return true comment out, I am not sure how to handle this; I know I don't do a return; what I want it it to submit the Dialog Box if length is 4, otherwise if it is 0, 1, 2, or 3, throw a toast. Where and how can I do that?

UPDATE: TO validate this preference, I need control of the OK button which I do not have; this may be a workaround.

Community
  • 1
  • 1
TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259

1 Answers1

2
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.

user2511882
  • 9,022
  • 10
  • 51
  • 59
  • That is close, but when you press the OK `button` in the `dialog`, it still does submit (the toasts are accurate though). I think the key is getting control of that OK button which is probably different then `EditTextPreference` which is more for the `EditText` view. – TheLettuceMaster Dec 28 '13 at 23:55
  • I dont see any dialog in the xml. What exactly is the problem? – user2511882 Dec 28 '13 at 23:56
  • With `EditTextPreference`, what it does when user clicks on it in Settings, it opens up a `dialog` with the `EditText` field, and two buttons: OK and Cancel. I didn't program this; it is default behavior in the xml. I think in the Java I have to find a way to access control of the `Button` for it. – TheLettuceMaster Dec 28 '13 at 23:59
  • I changed the answer. Try and see if it works for you – user2511882 Dec 29 '13 at 00:00
  • this.preference.setText(null); add this line to the code. Check the answer – user2511882 Dec 29 '13 at 00:03
  • Well if I enter 4 characters and hit OK, it closes fine; If I hit less than 4, I get a `NullPointer` on this line: `if (setPasscode.getText().toString().length() != 4) {`, with I'm assuming `setPasscode` is the culprit. – TheLettuceMaster Dec 29 '13 at 00:09
  • according to this question (http://stackoverflow.com/questions/7527894/how-to-call-the-ok-button-in-the-edittextpreference) , what I need can't be done with this method. I may need to make a custom preference. – TheLettuceMaster Dec 29 '13 at 00:11
  • Ok the last edit didn't crash it, but the dialog still closes. Still not sure we can access control of that button. Though this guy says this method worked but I'm not so sure .. http://stackoverflow.com/questions/15024692/android-validating-routine-for-edittextpreference – TheLettuceMaster Dec 29 '13 at 00:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44077/discussion-between-user2511882-and-kickinglettuce) – user2511882 Dec 29 '13 at 00:19