Android Hide Soft Keyboard from EditText while not losing cursor

Stephan Steiner picture Stephan Steiner · Nov 27, 2012 · Viewed 36.2k times · Source

I've come about as far as this which gets me halfway there, but not quite. I have a dialer Fragment that has all the usual Buttons to enter a number including backspace, so I don't need the soft keyboard. I'd also like to give the user the ability to paste text (long click... works fine per default), as well as to edit what has been entered so I need the cursor.

The easiest way I found to make sure the soft keyboard doesn't pop up if the user clicks inside the EditText is to set the inputType to null - but that kills the cursor as well.

So, how do I declare my EditText and what kind of commands should I launch to have my EditText field never ever show the soft keyboard no matter what the user attempts, but still retain paste functionality and the cursor?

I've also tried android:windowSoftInputMode="stateAlwaysHidden" in my manifest, but to no avail.

Answer

Eddie Sullivan picture Eddie Sullivan · Dec 20, 2012

This worked for me:

        // Update the EditText so it won't popup Android's own keyboard, since I have my own.
    EditText editText = (EditText)findViewById(R.id.edit_mine);
    editText.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.onTouchEvent(event);
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }                
            return true;
        }
    });