DarkLeafyGreen
DarkLeafyGreen

Reputation: 70416

Android NumberPicker setMin/Max

I try to set min/max on NumberPicker. I have following values:

One, Two, Three, Four

I set

picker.setMinValue(0);
picker.setMaxValue(1);

the picker will display

One, Two

However when I set

picker.setMinValue(1);
picker.setMaxValue(1);

it displays One but I expect Two. I am confused. Why is One displayed instead?

Upvotes: 0

Views: 2041

Answers (2)

Mattia Maestrini
Mattia Maestrini

Reputation: 32780

it displays One but I expect Two. I am confused. Why is One displayed instead?

Because NumberPicker works a little different from what you think.

In number picker you set the value with a code like this:

numberPicker.setMinValue(7);
numberPicker.setMaxValue(10);

Your NumberPicker will have the numbers 7, 8, 9, 10. Then use setDisplayedValues:

numberPicker.setDisplayedValues(new String[]{"One", "Two", "Three", "Four"});

The NumberPicker has the strings "One", "Two", "Three", "Four".

Now change the min value :

numberPicker.setMinValue(10);
numberPicker.setMaxValue(10);

The NumberPicker has the strings "One".

Why is that?

Because there is no correlation from real values and displayed values. As you can see in the source code of NumberPicker this is how the displayed text is calculated:

String text = (mDisplayedValues == null) ? formatNumber(mValue) : mDisplayedValues[mValue - mMinValue];

So if your min value is 10 and the selected value is 10 the displayed text is the first element of the array.

Returning to your example if you want to display the string "Two" when set min and max value to 1 you need to change the array accordingly:

numberPicker.setDisplayedValues(new String[]{"Two"});
picker.setMinValue(1);
picker.setMaxValue(1);

Upvotes: 4

Ashish Rawat
Ashish Rawat

Reputation: 5829

picker.setMaxValue(2) should give two, if max is 1 how can u expect 2

Upvotes: 0

Related Questions