Search code examples
androidjquery-animatenumberpickersetvalueandroid-wheel

Android NumberPicker animate wheel when using setValue method


i m trying to get the wheel of the numberPicker animated. I found the corresponding question here:

How to change NumberPicker's value with animation?

But i cannot solve it with the suppposed answers. Can somebody please help me?

When changing the value of the numberPicker from lets say 100 to 200, i want the wheel to increment from the old value to the new one. The wheel should slide to the new value. In the link passsy solved it with reflection but i don t know how to use it in practice..


Solution

  • It's really simple if you want to increment the value by one.

    just call changeValueByOne(myNumberPicker, true); to increment the numberpicker by one with animation. (false to decrement)

    increment from 100-200 is not possible with this method

                /**
                 * using reflection to change the value because
                 * changeValueByOne is a private function and setValue
                 * doesn't call the onValueChange listener.
                 * 
                 * @param higherPicker
                 *            the higher picker
                 * @param increment
                 *            the increment
                 */
                private void changeValueByOne(final NumberPicker higherPicker, final boolean increment) {
    
                Method method;
                try {
                    // refelction call for
                    // higherPicker.changeValueByOne(true);
                    method = higherPicker.getClass().getDeclaredMethod("changeValueByOne", boolean.class);
                    method.setAccessible(true);
                    method.invoke(higherPicker, increment);
    
                } catch (final NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (final IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (final IllegalAccessException e) {
                    e.printStackTrace();
                } catch (final InvocationTargetException e) {
                    e.printStackTrace();
                }
            }