Is it possible to set the increase/decrease interval of NumberPicker to other value? For instance to 50 (50, 100, 150, 200, 250 etc)? Or do it require some customization/hack?
4 Answers
I think you can do this:
Initialise a displayed array values for your picker :
int NUMBER_OF_VALUES = 10; //num of values in the picker int PICKER_RANGE = 50; ... String[] displayedValues = new String[NUMBER_OF_VALUES]; //Populate the array for(int i=0; i<NUMBER_OF_VALUES; i++) displayedValues[i] = String.valueOf(PICKER_RANGE * (i+1)); /* OR: if the array is easy to be hard-coded, then just hard-code it: String[] displayedValues = {"50", "100", "150", .....}; */
Set the displayed array to your picker :
/* NOTE: Your picker range has to be [0, displayedValues.size()-1] to be consistent with the array index (displayedValues index). */ //ToDo: Initialize the picker first. numPicker.setMinValue(0); numPicker.setMaxValue(displayedValues.size()-1); numPicker.setDisplayedValues(displayedValues);
When you want to get/set the value of the picker :
//To get the current value in the picker choosenValue = displayedValues[numPicker.getValue()]; //To set a new value (let's say 150) for( int i=0; i<displayedValues.length ; i++ ) if( displayedValues[i].equals("150") ) numPicker.setValue(i);
Finally, there is a great easy-to-customize widget called android-wheel. You can check it out and use it if you like.

- 5,818
- 5
- 42
- 58

- 16,292
- 20
- 87
- 132
-
Thanks for your reply and suggested solution. However I would not like to restrict the user from putting any precisely value in the picker. For instance if you would like to put value 47 or 48 you should be free doing that. This is also default behavior of the number picker if you double tap the picker. – Ismar Slomic Aug 13 '12 at 06:38
-
So, your question is not about `increase/decrease interval of NumberPicker`?! – iTurki Aug 13 '12 at 07:23
-
Sure it is. What i want is to preset the interval of increase / decrease when scrolling or using the up/down arrows of NumberPicker. But If you as user want to put an more precise value, between those intervals you should be allowed to do so, as the default behavior of NumberPicker. I haven't tested your solution yet, so it might be that it works as described here. – Ismar Slomic Aug 13 '12 at 07:28
@ iturki's last point setting a value. Because valueInNumberPicker X PICKER_RANGE = valueInDisplayedValueArray So we can avoid the for loop by:
// PICKER_RANGE= 50 so 150/50 -1 = 2, numPicker.setValue(2) == 150
numPicker.setValue(150/PICKER_RANGE -1);
My 2 cents.
By the way, great answer by iturki.

- 691
- 8
- 9
I have used the onScrollListener for scrolling with different step sizes and the onValueChanged for handling direct input with the number keyboard. For the use you need the variable oldValue which is available in both listeners.
int oldValue = 0;
In the onScrollListener I used the STATE_IDLE just because it works and i don t understand 100 % the difference for this use between all three...think you can use them either for this purpose.
You need the global oldValue variable to know the old value before scrolling. I haven t found a other solution. The if clauses is the min (0) and max (1000) boarder.
speedPicker.setValue(oldValue);
vitess.setProgress(oldValue);
((MainActivity) getActivity()).setSpeed(oldValue);
These three calls update my elements (vitess is a coupled progressBar, speedPicker is the numberPicker).
In the onValueChange I update also the elements and i set the oldValue.
By the way stepSize here is 100.
speedPicker.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChange(NumberPicker speedPicker, int scrollState) {
if (scrollState == NumberPicker.OnScrollListener.SCROLL_STATE_IDLE) {
if (speedPicker.getValue() < oldValue){
if (speedPicker.getValue() >= 100)
oldValue-=100;
else
oldValue = 0;
}
else{
if (speedPicker.getValue() <= 900)
oldValue+=100;
else
oldValue = 1000;
}
speedPicker.setValue(oldValue);
vitess.setProgress(oldValue);
((MainActivity) getActivity()).setSpeed(oldValue);
}
else if (scrollState == NumberPicker.OnScrollListener.SCROLL_STATE_FLING) {
}
else if (scrollState == NumberPicker.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
}
}
});
speedPicker.setOnValueChangedListener(new OnValueChangeListener() {
public void onValueChange(NumberPicker speedPicker, int oldVal, int newVal) {
oldValue = oldVal;
speedPicker.setValue(newVal);
vitess.setProgress(newVal);
((MainActivity) getActivity()).setSpeed(newVal);
}
});
This code works after some testing quit good for me.
This answer was inspired by jpintado. See his answer in Android NumberPicker OnValueChangeListener

- 1
- 1

- 43
- 1
- 8
-
But I have to add something in the beginning the first let s say 4-5 in/decreases by 1 are recognized by the onValueChange! And after the that the value changes by 100. – KelvinGradCelsius Jul 19 '13 at 10:21
A bit late in the day, but this works for me...
numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker numberPicker, int oldVal, int newVal) {
if (newVal > oldVal) {
numberPicker.setValue(newVal+(myInterval-1));
} else if (newVal < oldVal)
numberPicker.setValue(newVal-(myInterval-1));
}
});

- 805
- 1
- 10
- 16