prostock
prostock

Reputation: 9545

android softkeyboard showSoftInput vs toggleSoftInput

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

Upvotes: 38

Views: 30827

Answers (8)

Zoio
Zoio

Reputation: 1

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

Upvotes: 0

coeo15150
coeo15150

Reputation: 51

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

Upvotes: 5

Phil Allen
Phil Allen

Reputation: 171

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

Upvotes: 1

Guest
Guest

Reputation: 21

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

Android show softkeyboard with showSoftInput is not working?

Upvotes: 2

Mazen K
Mazen K

Reputation: 3662

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

Upvotes: 6

user_CC
user_CC

Reputation: 4776

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.

Upvotes: 2

Bert Regelink
Bert Regelink

Reputation: 2716

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

Upvotes: 27

Laura
Laura

Reputation: 2803

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

Upvotes: 1

Related Questions