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.
something like this should help you. Do keep in mind that
getPreferenceScreen
is deprecated, it is recommended to usePreferenceFragment
. I am assuming thatPreferenceActivity
is being extended here.