2

How can i change the increment of the NumberPicker from 1 to a number you want like 1000?

numberPicker.setMinValue(1000);
numberPicker.setMaxValue(15000);

This is how I set my NumberPicker and I dont want to increment it by 1 because it would take you longer time to reach like 8000 since the steps are by 1.

I have tried this one, numberPicker.setDisplayedValues(somethig_array) but with this i still have to provide all values with display values. Still not for me.

And lastly I tried using the OnValueChangeListener from this answer :

numberPicker.setOnValueChangeListener(new NumberPicker.OnValueChangeListener() {

 @Override
 public void onChange(NumberPicker picker, int oldVal, int newVal) {
     picker.setValue((newVal < oldVal)?oldVal-1000:oldVal+1000);
 }
    });

Even though the next number of 1000 is 2000, but still it will display the 10001-1999. Is there any way to modify the increments of the NumberPicker ?

Any idea is greatly appreciated.

Community
  • 1
  • 1
elL
  • 767
  • 2
  • 14
  • 35

1 Answers1

11

The setDisplayedValues() is what you want. A simple for-loop can help you create the array very simply.

int minValue = 1000;
int maxValue = 15000;
int step = 1000;

String[] valueSet = new String[maxValue/minValue];

for (int i = minValue; i <= maxValue; i += step) {
    valueSet[(i/step)-1] = String.valueOf(i);
}
nomachinez
  • 521
  • 4
  • 6