Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing NumberPicker scrolling speed

Can't believe I haven't found an answered Stack Overflow post about this...

I'm making a timer where you select the time from a NumberPicker widget. The problem is that it takes 10-13 scrolls to get to the bottom

I looked in the documentation but haven't found anything

String[] minsecvalues = new String[61];

for(int i=0; i < minsecvalues.length; i++){
        minsecvalues[i] = Integer.toString(i);
}

NumberPicker mSecondsPicker = (NumberPicker) v.findViewById(R.id.np_seconds_picker);
mSecondsPicker.setMaxValue(60);
mSecondsPicker.setMinValue(0);
mSecondsPicker.setWrapSelectorWheel(true);
mSecondsPicker.setDisplayedValues(minsecvalues);

//supposed to change scroll speed but doesn't work
mSecondsPicker.setOnLongPressUpdateInterval(8000);
//This method looks promising but my app crashes when I run it
//mSecondsPicker.scrollBy(0, 20);
like image 261
Claudiu Moise Avatar asked Mar 21 '26 22:03

Claudiu Moise


2 Answers

There is no public way to access friction level in the default NumberPicker widget.

You can use this custom NumberPickerView. Installation instructions in english are here.

It does exactly what you want with the method below to control friction level:

//double the default scroll friction level
mNumberPickerView.setFriction(2 * ViewConfiguration.get(mContext).getScrollFriction());

The default scroll friction value is 0.015f and can be found here

like image 179
MatPag Avatar answered Mar 23 '26 12:03

MatPag


I have seen a few questions relating to this point (such as here and here).

The variable in NumberPicker associated with maximum scroll speed is set to private by default. Fortunately, we can use reflection to access and change these values. Why the class doesn't have a public method for altering something as basic as scroll speed is beyond me.

Digging around in the source code for NumberPicker here I found this to be the private int mMaximumFlingVelocity variable.

Assuming numberPicker is our NumberPicker:

// Can set this value to anything to modify our scroll speed, where a value
// between 0 and 1 makes the scroll speed slower, and anything greater than
// 1 makes it faster (e.g. a factor of 2 makes it twice as fast)
final double scrollSpeedFactor = 2;

// Surrounding by try/catch is required here because Class.getDeclaredField
// can throw a NoSuchFieldException and Field.get can throw an IllegalAccessException
try {
    // Our private field is accessed with getDeclaredField
    Field field = numberPicker.getClass().getDeclaredField("mMaximumFlingVelocity");

    // We make the field accessible
    field.setAccessible(true);

    // Get the value
    int maxVelocityValue = (int) field.get(numberPicker);

    // Set a new velocity value based on our scrollSpeedFactor
    int newVelocityValue = (int) (maxVelocityValue * scrollSpeedFactor);

    // Set the new velocity
    field.setInt(numberPicker, newVelocityValue);
} 
catch (NoSuchFieldException | IllegalAccessException e) {
    e.printStackTrace();
}
like image 34
Dantic Avatar answered Mar 23 '26 12:03

Dantic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!