views:

58

answers:

1

What is the best way of configuring a widget in android for entering a 4 digit PIN?

Currently I have:

<EditTextPreference 
  android:title="PIN" 
  android:summary="Your PIN number"
  android:key="password"
  android:numeric="integer"
  android:maxLength="4"
  android:password="true"
/>

This works quite well, but there is no way of constraining the input to be at least 4 digits. Is this how you would have someone set a PIN in preferences? Or is there a better way?

By the way, before people comment on security implications, I didn't choose the 4 digit PIN, it is for connecting to a third party system and I can't change it. And no, this isn't for banking.

+1  A: 

You can add an OnPreferenceChangeListener and check the password length there.

E.g. if using a PreferenceActivity:

findPreference("password").setOnPreferenceChangeListener( new OnPreferenceChangeListener(){
    public boolean  onPreferenceChange  (Preference preference, Object newValue){
        if ((String)newValue).length == 4)
            super.onPreferenceChange(preference, newValue);
        else
            Toast.makeText(this, "The PIN must have 4 digits", Toast.LENGTH_LONG);
    }
});
Thorstenvv