How to get the value of an instance of NumberPicker from onValueChange when the NumberPicker doesn't scroll (by clicking the plus or minus buttons) in Android

I have an instance of NumberPicker and it works as expected, but I need to get its value when scrolling stops. Or when the user only clicks the "Increments / decrements" button. the value I should get is displayed in the text view.

I managed to get the value when the scroll stopped / became inactive and displayed correctly (this is done in the onScrollChanged()listener).

but the listener onScrollChanged()will not start when the user presses the increase or decrease buttons, and if I use the onValueChanged()listener, the text is updated, but the scrolling becomes very slow and slow (as a result of updating all the time when the value is changed).

So I need help how to display the NumberPicker value all the time without causing the NumberPicker to delay or slow down.

The code is disabled as follows:

    NumberPicker milisecoundsNumberPicker = (NumberPicker) findViewById(R.id.numberPicker);
    milisecNumberPicker.setFocusable(false);
    milisecNumberPicker.setFocusableInTouchMode(false);
    TextView textNumber = (TextView) findViewById(R.id.selectednum);



    milisecNumberPicker.setMaxValue(10);
    milisecNumberPicker.setMinValue(0);
    milisecNumberPicker.setWrapSelectorWheel(false);
    String[] nums = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
    milisecNumberPicker.setDisplayedValues(nums);// array of numbers
    milisecNumberPicker.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScrollStateChange(NumberPicker view, int scrollState) {

            if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
                    || scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                view.getValue();

                textNumber.setText(view.getValue() + "");
 //this is ok no lagging but will not work on pressing increments and decrements buttons    
            }

        }
    }); 
// If I use this
    milisecNumberPicker.setOnValueChangedListener(new OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) { 
                    // set Text value here causing lagging on scrolling                                                             
        }
    });

Sorry for the poor formatting. I am new to this, but can anyone help me in this thread.

+1
source share
1 answer

Hi

import android.os.Handler;

...............

milisecoundsNumberPicker.setOnValueChangedListener(new android.widget.NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(android.widget.NumberPicker picker, final int oldVal, final int newVal) {
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    if (newVal == milisecoundsNumberPicker.getValue()) {
                        textNumber.setText(newVal + "");
                    }
                }
            }, 500);//set time 
        }
    });
+1
source

All Articles