38

showSoftInput() doesn't show the keyboard for me, but toggleSoftInput() does. I saw some other post that said to disable the hard keyboard when using the emulator, but I'm not using an emulator. I'm loading my APK on an actual device with no hard keyboard. Shouldn't both methods work? Why doesn't showSoftInput() work? I would like to explicitly associate the keyboard with a specific text field.

Doesn't work:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.setText("textchange"); //i see the text field update
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);

Works:

InputMethodManager imm = (InputMethodManager) getDelegate().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
1
  • 1
    Have you figured this out? I've spent 3 days on this problem already. If I try to use toggle, the keyboard will sometimes get hidden, since it's already visible. And of course Android doesn't think it's necessary to give us the ability to query whether the softinput is visible or not.
    – bgolson
    Commented May 7, 2013 at 17:31

8 Answers 8

27

It seems that the keyboard is initially displayed but hidden by something else, because the following works (but is actually a dirty work-around):

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.postDelayed(new Runnable()
{
    @Override
    public void run()
    {
        editText.requestFocus();
        imm.showSoftInput(editText, 0);
    }
}, 100);

And when looking at logcat I suspect the cause behind this message hides the keyboard initially shown:

Hide Clipboard dialog at Starting input: finished by someone else... !

2
  • 3
    That's actually the only thing that worked for me when trying to show the keyboard programatically in onCreate. I wish I'd know why. thanks anyway.. Commented May 26, 2013 at 14:00
  • 1
    Yepp. This is the only thing that worked correctly for me. Commented Oct 5, 2016 at 13:39
6

Show Keyboard + focus and also if you want to Hide the keyboard

@Override
public void onResume () {
    super.onResume();

    inputSearch.setFocusableInTouchMode(true);
    inputSearch.requestFocus();

    // Show Keyboard
    InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(
            Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(inputSearch, InputMethodManager.SHOW_IMPLICIT);
}

P.S inputSearch = (EditText) getSherlockActivity().findViewById(R.id.inputSearch);

    // Hide Keyboard
InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);
0
5

ShowSoftInput works if the imm's target view is same with the editText. You can check this by imm.isActive(editText) or editText.isInputMethodTarget().

ToggleSoftInput is just toggling the keyboard of the current target of imm.

Target view of imm is set after the focus has been changed by editText.requestFocus(). I think some post processes exist between these two tasks since one post runnable was not enough. I tested double post and it worked for me.

editText.requestFocus();
editText.post(new Runnable() {
    @Override
    public void run() {
        editText.post(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.showSoftInput(editText, 0);
                }
            }
        });
    }
});
2
  • 1
    "ShowSoftInput works if the imm's target view is same with the editText. You can check this by imm.isActive(editText) or editText.isInputMethodTarget(). ToggleSoftInput is just toggling the keyboard of the current target of imm." Thank you for this
    – Melllvar
    Commented Oct 14, 2018 at 7:30
  • Thanks ! Didn't really understood why but your solution works perfectly ! Commented Nov 26, 2019 at 5:03
2

The precise answer to this question why does showSoftInput doesnt work and toggleSoftInput does?

Is that the View to which you are trying to display the Keyboard doesn't have the focus. So to solve this problem and to use the method showSoftInput you will have to call the following to methods on your view:

  setFocusable(true);
  setFocusableInTouchMode(true); 

Calling the above methods will make sure that when you click on the View retains and captures the focus.

1
  • 1
    The precise answer should at least show an example... because it didn't work! Commented Jul 18, 2013 at 4:39
2

showSoftInput() does not work when device has a hard (slide-out) keyboard

Android show softkeyboard with showSoftInput is not working?

1

Try this:

public void showTheKeyboard(Context context, EditText editText){
    InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}

If this doesn't work read the tutorial from here

1
  • 2
    i had to Toggle. For some reason showSoftInput didn't work for me
    – prostock
    Commented Dec 5, 2012 at 20:43
1
public void hideKeyboard() {
    myTextView.setFocusable(true);
    myTextView.setFocusableInTouchMode(true);
    imm.hideSoftInputFromWindow(myTextView.getWindowToken(), 0);
}

WORKS

public void hideKeyboard() {
    imm.hideSoftInputFromWindow(myTextView.getWindowToken(), 0);
}

DOES NOT WORK

imm is dealt with earlier as I am using a Fragment so:

Declare imm in the Fragment

private InputMethodManager imm;

Then in the fragment add:

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    imm = (InputMethodManager)
    getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
}

He says after 3 to 4 hours of research and failures !!

Thanks user_CC ! :-)

Phil

1
  • 1
    FWIW, Had to read this twice before I was certain that it was the first code snippet that works - would have been clearer if put "WORKS:" ABOVE the first snippet, and "DOES NOT WORK:" ABOVE the second snippet. Commented Nov 13, 2015 at 3:03
0

I know this post is pretty old, but for those who came for answers from this date and none of the above worked. The code below worked for me on an Activity when an Alert Dialog popped up.

InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

to show keyboard:

keyboard.toggleSoftInput(editText.getPaintFlags(), 0);

to hide keyboard:

keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);

Not the answer you're looking for? Browse other questions tagged or ask your own question.