2

I have A array that have negative and positive values, but when I start the app I only can see the minimum value in the picker.

int min = -25;
int max = 100;
int jumpBy = 5;

        String [] refl_Str = new String[((max-min)/jumpBy)+1];


        for (int i = 0; i < refl_Str.length; i++) 
        {
            refl_Str[i] = Integer.toString(min+(i*jumpBy));


        }

        np.setDisplayedValues(refl_Str);

When I add:

np.setMaxValue(100);
np.setMinValue(-25);

I get error on negative values in the numberpicker.

Dim
  • 4,527
  • 15
  • 80
  • 139
  • Possible duplicate of [Android NumberPicker - Negative Numbers](http://stackoverflow.com/questions/14357520/android-numberpicker-negative-numbers) – user276648 Jun 29 '16 at 02:43

1 Answers1

2

you can use following code to do that:

final int minValue = -25
final int maxValue = 100
NumberPicker numberPicker = new NumberPicker(myActivity);
numberPicker.setMinValue(0);
numberPicker.setMaxValue(maxValue - minValue);
numberPicker.setValue(myCurrentValue - minValue);
numberPicker.setFormatter(new NumberPicker.Formatter() {
    @Override
    public String format(int index) {
        return Integer.toString(index - minValue);
    }
});

then to get back the selected value:

int myNewValue = numberPicker.getValue() + minValue
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • What about jumping by 5? – Dim Jan 07 '14 at 09:59
  • this is just sample that want to say, you must setMinValue to `0` and setMaxValue to `maxValue - minValue`, other thing you can handled like your code – Shayan Pourvatan Jan 07 '14 at 10:02
  • 2
    On Nexus5 and Lollipop, this does not work. Just I move the selector wheel, numbers jumps of -minvalue. – ARLabs Apr 19 '15 at 05:55
  • As @ARLabs mentioned, now the NumberPicker parses the current value as text when scrolling and sets the value to the parsed value, not the `myCurrentValue - minValue` type of value that we want. – Mitch Mar 15 '18 at 06:08