Setting Increase / Decrease NumberPicker Interval

Is it possible to set the increment / decrement interval of NumberPicker to another value? For example, up to 50 (50, 100, 150, 200, 250, etc.)? Or does it require some tweaking / hacking?

+5
source share
3 answers

I think you can do this:

  • Initialize the displayed array values for your collector:

    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 collector:

    /* 
    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);
    
  • If you want to get / set the value of the collector:

    //To get the current value in the picker
    choosenValue = displayedValues[numPicker.getValue()]; 
    //To set a new value (let say 150)
    for( int i=0; i<displayedValues.length ; i++ )
        if( displayedValues[i].equals("150") )
             numPicker.setValue(i);
    

Finally, there is a large, easy-to-configure widget android-wheel . You can check it and use if you want.

+6
source

@iturki . valueInNumberPicker X PICKER_RANGE = InDisplayedValueArray for:

// PICKER_RANGE= 50 so 150/50 -1 = 2, numPicker.setValue(2) == 150
numPicker.setValue(150/PICKER_RANGE -1); 

2 .

, iturki.

0

onScrollListener onValueChanged . oldValue, .

int oldValue = 0;

onScrollListener STATE_IDLE , , 100% ... , .

oldValue, . . if - min (0) max (1000).

speedPicker.setValue(oldValue);
vitess.setProgress(oldValue);
((MainActivity) getActivity()).setSpeed(oldValue);

(vitess - Bar, speedPicker - numberPicker).

In onValueChange, I also update the elements and set oldValue.

By the way, stepSize is 100 here.

    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 tests remain useful to me.

This answer was inspired by jpintado. See His Answer in Android NumberPicker OnValueChangeListener

0
source

All Articles