Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NumberPicker in AlertDialog always activates keyboard. How to disable this?

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

like image 786
barrel Avatar asked Jun 12 '12 12:06

barrel


3 Answers

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!!!! :-)

like image 95
barrel Avatar answered Nov 11 '22 23:11

barrel


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 ;-)

like image 35
pawelzieba Avatar answered Nov 12 '22 00:11

pawelzieba


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.

like image 1
Jave Avatar answered Nov 11 '22 23:11

Jave