I'm having trouble correctly editing the editing field of my NumberPicker , I read some useful answers to similar questions, but none of them seem to address this specific issue. Although the increase and decrease buttons work correctly if I enter a number using the soft keyboard, I cannot get this value using the NumberPicker.getValue() method. I tried using my own NumberPicker.OnValueChangedListener() , but it is not called unless I press the up or down buttons, and not when I press the Finish button on a soft keyboard, as some have suggested. Can someone help me with this? Is this an Android bug?
Here is my XML layout:
<LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/textView1" android:layout_weight="1" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:text="@string/enter_nights_" android:layout_width="0dp" android:padding="10dip" android:textColor="@color/black" ></TextView> <NumberPicker android:id="@+id/numPick" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
and my initialization code from my Activity onCreate ():
mNumNights = (NumberPicker) findViewById(R.id.numPick); mNumNights.setMinValue(1); mNumNights.setMaxValue(99); mNumNights.setValue(mNights); mNumNights.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
And when I exit Activity, I get the following value:
mNights = mNumNights.getValue();
I do not see any other methods in the Android Documentation on NumberPicker . I saw a message suggesting to connect onEditorActionListener() to get the Finish button, however this does not tell me how to get the value! So, to summarize, my question is how can I get the value and why doesn't NumberPicker automatically update its value?
Thanks!
EDIT: I stumbled upon this SO question , which is very similar to my question, the answers there look useful, although I hope there is a better approach than suggested.
source share