Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you display a 2 digit NumberPicker in android?

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.

like image 607
illis69 Avatar asked Nov 02 '13 00:11

illis69


3 Answers

    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

like image 184
Naveen Agrawal Avatar answered Oct 04 '22 08:10

Naveen Agrawal


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

like image 34
jspurlock Avatar answered Oct 04 '22 09:10

jspurlock


 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);
            }
        });
like image 2
Vinayak Avatar answered Oct 04 '22 08:10

Vinayak