Android – show and hide soft keyboard programmaticaly – Mea Cup O' Jo
Skip to content


Android – show and hide soft keyboard programmaticaly

I had to figure it out today so I thought I’ll shared it with the world. So say for whatever reason you need to show or to hide a virtual (soft) keyboard in Android app. It’s not very intuitive but can be easily done:
To show

((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
		        .showSoftInput(editText, 0);

And to hide:

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

Posted in Android.


17 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Dordesseshy says

    Could not find a suitable section so I written here, how to become a moderator for your forum, that need for this?

    • droidin says

      This is a private one-man blog, sorry but I can't grant you or anyone else moderator privileges. You can certainly write me if you find any errors or you can edit your own comments

  2. Zeba says

    It didnt work for me!!! :(
    code:
    EditText etName = (EditText) dialog.findViewById(R.id.txtName);
    ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
    .showSoftInput(etName, 0);

    Wats wrong here?

  3. Profete162 says

    Does not work for me neither. I wrote the code at the end of the onCreate of my activity and the keyboard always show at the start of this activity.

  4. YBSoUpset? says

    Didn't work for me either. I had to set editor input to NULL, then re-set it correctly upon touch. Like this (where testcaseText is my EditText):
    testcaseText.setInputType(InputType.TYPE_NULL); // disable soft input
    testcaseText.setOnTouchListener(new View.OnTouchListener(){
    public boolean onTouch(View v, MotionEvent event) {
    testcaseText.setInputType(InputType.TYPE_CLASS_NUMBER);
    testcaseText.onTouchEvent(event); // call native handler
    return true; // consume touch even
    }

  5. Joe Masilotti says

    If you are experimenting with this in the emulator, there is a good chance it won't work by default.

    When creating a new emulator Virtual Device, you have to make sure to disable the physical keyboard. The soft keyboard won't show when the physical one is available.

    Hope this helps!

  6. che says

    That's what I'm searching for. Thank you YBSoUpset.

  7. celsius says

    Works perfectly for me!

  8. Mike says

    Wow! I have looked at at least 40 different approaches to this and yours is the first one that worked and is succinct as well!!!

    Thanks,

    I know this has nothing to do with this, but I have a question. I am trying to use setKeyListener to format the data a user enters. The problem I have is that once a user presses the delete key, the next keytap does not appear.

    I am using setText() to reset the display and that is what causes the problem, but without the setText(), there is no need to use the KeyListener.

  9. Xavier says

    Thank YOU!!!!!!!!!!!!!!!!!!
    I've been looking for this for hours!!!

    CHEARS MATE!

  10. Chad Hedgcock says

    If you want the keyboard to show up as soon as the Activity launches, then put android:windowSoftInputMode="stateVisible" in the manifest as an attribute for that Activity. Then set the focus to the EditText that you want the keyboard to show up for:

    View exampleView = (View)findViewById(R.id.exampleBox);
    exampleView.requestFocus();

  11. Oeyvind says

    Thanks a lot!
    Had almost given up hope of finding out how to do this and finally found this post!

  12. Android says

    If you are not getting the solution using this code means, you can force it to show, the code is on this site http://android-codes-examples.blogspot.com/2011/1...

  13. CRUNCHDROID says

    Or Very simple use this :
    <activity name="EditContactActivity"
    android:windowSoftInputMode="stateVisible|adjustResize">

    </activity>

  14. Hal says

    Doesn't work on a tablet either.



Some HTML is OK

or, reply to this post via trackback.