Passwords are not nice, are not easy, and cause headaches.

Unfortunately, they are what anyone who is offering a service to us wants us to use. A few years ago I got fed-up with my password management strategy (I didn’t have one), and investigated the possibilities. I settled on generating an (essentially) gibberish string and memorizing it in a somewhat obfuscated way… (I write them down, so shoot me!) To aid this I created a nice powershell function:

And I usually call it thusly:

1..10 | %{ New-Password }

And this works nicely, generating 10 passwords that I can choose from, but it does have a couple of limitations:

  • Characters can be (and usually are) repeated
  • It’s a powershell function (so you need to already be logged in and have bits of my profile setup in powershell)
  • And I’m sure there are others

As I’ve already posted I’ve been looking at Android development, so why not create a small app to generate passwords automatically? Crucially by doing it in an Android app I would then have something with me most of the time where I can get a new password. Nice! So I quickly scratched down a design for my very basic app:

Main screen Main screen

And such works of art they are!

So with Android Studio fired up, and a pristine new project loaded off I set. One of the important (to me) parts of the powershell function is that I’m able to specify complexity and length when necessary. So I decided to get the settings preferences working first. And here Google’s own tutorial came up trumps. Creating an initial set of preferences

and getting them up on screen

Initial preferences screen

was pretty straightforward. Unfortunately there were a couple of problems: the length allowed anything; and the summary for the length was not working correctly.

Initial preferences screen

Going through the tutorial led me to building a custom preference, which is fine, though work that I felt sure that someone else had already done. Some judicious searching revealed that Niklas Baudy was way ahead of me. Integrating the VNTNumberPickerPreference in to my project really was as straightforward as README.md suggests:

Updating the preferences xml was also straightforward:

One problem I did encounter was that I had re-used the preference name, so it already existed, as a string, which caused some crashes. Once I engaged brain and read the error message in the console that was quickly solved by uninstalling the app from the emulator, and then running again.

By default VNTNumberPickerPreference provides a simple summary of the selected value. But my design (ha!) called for something a little more sophisticated. Looking at the sample application revealed that I should be implementing SharedPreferences.OnSharedPreferenceChangeListener and overriding onSharedPreferenceChanged:

I did try implementing Preference.OnPreferenceChangeListener and overriding onPreferenceChange but although this gets called the summary set by Preference itself is used.

The completed preference screen now looks like this.

Preferences screen with custom summary Preferences screen with number selector