Forcing the Soft Keyboard open

I am trying to force the Soft Keyboard open in an Activity and grab everything that is entered as I want to handle the input myself, I don't have an EditText. Currently I have tried this but it does not work. I would like the Soft Keyboardto open below mAnswerTextView (Note: it is a TextView not EditText).

    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    // only will trigger it if no physical keyboard is open
    mgr.showSoftInput(mAnswerTextView, InputMethodManager.SHOW_IMPLICIT);
  1. how do I force the Soft Keyboard open
  2. How do I gab everything that is entered so that I can handle each character. I would like to flush each character from the Soft Keyboard after I have handled it. ie, the user should not be able to enter whole words in the Soft Keyboard.

Solution 1:

try this to force open soft keyboard:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

then you can to use this code to close the keyboard:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(_pay_box_helper.getWindowToken(), 0);

Solution 2:

You'll probably need to have an editable text area of some kind to take focus. You can probably have one invisible or on a transparent background with no cursor, though. You may need to toy around with the focusability settings for the view.

Use a TextWatcher to check for edits to that EditText with addTextChangedListener, or if you need an even-lower-level hook, set the textview's key listener with its setOnKeyListener() method. See the KeyListener documentation.

Use this call to force the soft keyboard open:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
    .showSoftInput(myEditText, InputMethodManager.SHOW_FORCED);

and this one to close it:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
    .hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

note that this is really not recommended - forcing the keyboard open is kind of messy. What's your use case that really necessitates your taking user input without a normal edit box and requires eating user input on a key-by-key basis without echoing it back?

Solution 3:

To force the keyboard to open I used

this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);

it worked for me.

Solution 4:

Sometimes the other answers won't work.
Here is another way..

It will force the keyboard to show when the activity starts by listening to the window focus. onWindowFocusChanged() it will clear and request focus of the EditText, then set the soft input mode to visible and set the selection to the text in the box. This should always work if you are calling it from the activity.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        mEditText.clearFocus();
        mEditText.requestFocus();
        getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        mEditText.setSelection(mEditText.getText().toString().length());
    }
}

You may also need

mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
            }
        }
    });

Edit: I have also seen the keyboard not open inside nested fragments, beware of those kinds of situations.