(Android) NumberPicker with leading zero


Set your number picker to show leading zeros by applying a formatter to it.
Example is using an anonymous instance, but of course it doesn’t have to.

[java]
myNumberPicker.setFormatter(new NumberPicker.Formatter() {
@Override
public String format(int value) {
return String.format("%02d", value);
}
});

[/java]

For %02d this goes:

  • 0 for filling with zeros
  • 2 for length of 2

3 responses

  1. Many thanks, I’ve been struggling with how to do this for the last day.
    Cheers.

    1. Great to hear that it came to use!

  2. Stephane Schittly Avatar
    Stephane Schittly

    Thanks for the tip!

Leave a Reply

Your email address will not be published. Required fields are marked *