NumberPicker not designed for this interaction. When you change a value using the keyboard, you make changes directly to the widget from NumberPicker , and the widget itself does not see this change, so you end up with the last saved value. To get around this, you will need some hacked code, which is actually not recommended, because you need to access the basic EditText (provided that the phone manufacturer did not change this in the first place):
private EditText findInput(ViewGroup np) { int count = np.getChildCount(); for (int i = 0; i < count; i++) { final View child = np.getChildAt(i); if (child instanceof ViewGroup) { findInput((ViewGroup) child); } else if (child instanceof EditText) { return (EditText) child; } } return null; }
and then you will use this code to set the TextWatcher to the found EditText to see when it was manually changed (and let NumberPicker know about this change):
EditText input = findInput(np); TextWatcher tw = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) {} @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void afterTextChanged(Editable s) { if (s.toString().length() != 0) { Integer value = Integer.parseInt(s.toString()); if (value >= np.getMinValue()) { np.setValue(value); } } } }; input.addTextChangedListener(tw);
Another option is to make your own implementation of the NumberPicker widget and insert the target functions.
source share