NumberPicker's onClick listener takes multiple clicks to recognise

126 views Asked by At

I want to make sure that user clicks on the value in the number picker, and once done, I take an action. I've written the code below. The numberPicker is created in a bottomSheetDialogFragment.

nPicker.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    NumberPicker p = v.findViewById(R.id.numberpicker_picker);
    Log.e("picker selected", ""+p.getValue());
}

The above code works fine to an extent, but it have to click on the numberpicker a bunch of times before the click is recognised.

Does anyone know why that is and if there's a solution?

1

There are 1 answers

1
avalerio On

You may want to use the onValueChangeListener(). The clicks are probably getting consumed by this listener, so your onClick(View v) isn't working.

numberPicker.setOnValueChangedListener((numberPicker, oldValue, newValue) -> {
    Log.e("picker selected", ""+newValue);
});