To all,
I am trying to get a simple NumberPicker to work in a AlertDialog. The problem is that whenever I increase/decrease the value in the numberpicker, the keyboard activates.
There are many posts describing this problem, but none of the suggestions work. I have tried:
android:configChanges="keyboard|keyboardHidden"
And
inputManager.hideSoftInputFromWindow(currentView.getWindowToken(), 0);
And
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
I have tried calling these functions before and after initialization (dialog.show ()), on keypress events (using listeners obviously), etc. but no luck so far.
The complete code:
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myNumber"
android:configChanges="keyboard|keyboardHidden"
/>
</RelativeLayout>
And the calling function:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
View view = getLayoutInflater().inflate(R.layout.popup, null);
builder.setView (view);
final AlertDialog dialog = builder.create ();
NumberPicker picker = (NumberPicker) view.findViewById(R.id.myNumber);
picker.setMinValue(0);
picker.setMaxValue(999);
dialog.getWindow().
setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
dialog.show();
Any help appreciated
Barry
Actually, although the solution above works perfectly, there is an easier way.
picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
This is all that is needed. Anyway, thanks for the replies!!!! :-)
Since there is no access to NumberPicker buttons, it's "impossible" to do.
I made fast dirty hack to deal with it.
First add focus eater to layout, in this case Button with 0 size:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myNumber"
android:configChanges="keyboard|keyboardHidden"
/>
<Button
android:id="@+id/myBtn"
android:layout_width="0dp"
android:layout_height="0dp"
/>
</RelativeLayout>
We need to catch event of clicking on incement/decrement buttons, I chose OnValueChangedListener, couldn't find something better.
EditText edit = null;
try {
final Field[] fields = picker.getClass().getDeclaredFields();
for (Field f : fields) {
if (EditText.class.equals(f.getType())) {
f.setAccessible(true);
edit = (EditText) f.get(picker);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
if (edit != null) {
final EditText finalEdit = edit;
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
picker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker numberPicker, int i, int i1) {
dialog.findViewById(R.id.myBtn).requestFocusFromTouch();
imm.hideSoftInputFromWindow(finalEdit.getWindowToken(), 0);
}
});
}
This is not recommended solution. I hope someone would find something better. Use only for educational purpose ;-)
the android:configChanges
attribute belongs in your Manifest.xml
file, not the layout. But if you are hiding the keyboard from there, you are probably best of using
android:windowSoftInputMode="stateAlwaysHidden"
You should also try to use getWindow()
on your Activity
rather than just the Dialog
and see if that helps. This is probably the best solution as the first one (hiding from the manifest) will keep the keyboard hidden throughout the entire Activity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With