Home » Android » android softkeyboard showSoftInput vs toggleSoftInput

android softkeyboard showSoftInput vs toggleSoftInput

Posted by: admin April 23, 2020 Leave a comment

Questions:

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);
How to&Answers:

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… !

Answer:

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

Answer:

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.

Answer:

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

Android show softkeyboard with showSoftInput is not working?

Answer:

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

Answer:

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

Answer:

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