Search code examples
androidandroid-dialogandroid-dialogfragmentnumberpicker

How to read current value from NumberPicker?


I have a following problem. I have a NumberPicker located in the Android DialogFragment. I've made a code which sets its value to the previously set value (when you open the Dialog, NumberPicker is set to the previous value). It works fine, but there is one case in which it fails. When I open the Dialog and don't change NumberPicker value and close it, the NumberPicker always sets itself to the 0 value. It is beacuse newVal and oldVal in such case are always 0. Is there any other way to read NumberPicker's current value?

Fragment of my code:

npd.setOnValueChangedListener(( new NumberPicker.
        OnValueChangeListener() {
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                type = (sizes[newVal]);
                past = oldVal;
                last = newVal;
            }
        }));

oldVal and newVal are always zero when the value of NumberPicker is not changed (quite logical), but how to read NumberPicker's value in such case?


Solution

  • picker.getValue()
    

    should provide the current value.