Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PIN entry widget for android

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.

like image 249
Nick Fortescue Avatar asked Sep 05 '26 09:09

Nick Fortescue


1 Answers

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).show();
    }
});
like image 120
Thorstenvv Avatar answered Sep 08 '26 00:09

Thorstenvv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!