user3691763
user3691763

Reputation: 95

android number picker with undefined value

Is there a way to create a NumberPicker that has the numbers from 0 to 15, but where the user still can decide whether he really wants to set a number? So for example:

undefined (default)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
undefined... and so on

Or can you think of other user friendly methods to achieve this?

Upvotes: 0

Views: 415

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57173

Sure you can:

widget = (NumberPicker) findViewByid(R.id.mynp);
widget.setMaxValue(16);
widget.setMinValue(0);
String[] displayedValues = new String[widget.getMaxValue()-widget.getMinValue()+1];
for (int i=0; i<16; i++) {
    displayedValues[i] = String.format("%d", i);
}
displayedValues[16] = "undefined (default)";
widget.setDisplayedValues(displayedValues);
widget.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
    @Override
    public void onValueChange(NumberPicker picker, int oldValue, int newValue) {
        if (newValue > 15) {
            useDefaultValue();
        }
        else useValue(newValue);
    }
}

Upvotes: 3

Related Questions