16

I want to create a timer and since I couldn't create a digital interface that is editable for the user to set the time I want to use the NumberPicker. However the NumberPicker only displays 1 digit for the numbers between 0-9. How do you format the picker so that it will display two digits such as 01 02 03 and so forth.

illis69
  • 175
  • 1
  • 2
  • 9

3 Answers3

46
    numberPicker.setMaxValue(10);
    numberPicker.setMinValue(0);
    numberPicker.setFormatter(new NumberPicker.Formatter() {
        @Override
        public String format(int i) {
            return String.format("%02d", i);
        }
    });

This will do the trick!

enter image description here

Naveen Agrawal
  • 576
  • 5
  • 5
4

Implement a custom NumberPicker.Formatter that implements your value display padding and call setFormatter.

jspurlock
  • 1,466
  • 10
  • 7
  • 4
    Thanks I got it to work. `public class MyTwoDigitFormatter implements Formatter { public String format(int value) { return String.format("%02d", value); } }` – illis69 Nov 11 '13 at 20:33
3
 NumberPicker monthPicker = (NumberPicker) view.findViewById(R.id.np_month);
        monthPicker.setMinValue(1);
        monthPicker.setMaxValue(12);
        monthPicker.setFormatter(new NumberPicker.Formatter() {
            @Override
            public String format(int i) {
                return String.format("%02d", i);
            }
        });
Vinayak
  • 6,056
  • 1
  • 32
  • 30