How to Show/Hide Soft Keyboard Programmatically [DEV TIPS & TOOLS]

30

how_to_dev_tip_wrenchI had to figure it out today so I thought I’ll shared it with the world. Say for whatever reason you need to show or to hide virtual (soft) keyboard in your Android app. It’s not very intuitive but can be easily done. Here’s one scenario – you are executing action that places focus into the text field. To my surprise it doesn’t bring up a soft keyboard on MyTouch (or emulator). To actually do so requires the following additional code:

To show:

[java]
EditText editText = (EditText) findViewById(R.id.myEdit);
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
[/java]

And to hide:

[java]
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
[/java]

30 COMMENTS

  1. how would this be done for an input element in a WebView? I've tried basically the same code using the WebView but the soft keyboard does not show.

    • Filed in webview is rendered by engine off the HTML so the code above is not applicable. I actually found that for most parts OS is smart enough to bring the virtual keyboard when user clicks on the textfield in WebView

      • The WebView in my app never shows the keyboard when a textfield is clicked is there something special that i need to do for this to work?

  2. Not sure this works on all devices. I think it is best to test if mgr!=null before making the actual call, just to avoid an exception.

  3. Doesn't work for me 🙁
    code:
    EditText etName = (EditText) dialog.findViewById(R.id.txtName);
    ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
    .showSoftInput(etName, InputMethodManager.SHOW_IMPLICIT);

    Wats wrong??
    Plzz help..thank you.

  4. Zeba whats your error?

    probably you have to reference the code to… "this", are you working in a Dialog?

    EditText etName = (EditText) this.findViewById(R.id.txtName);

    or just

    EditText etName = (EditText) findViewById(R.id.txtName);

  5. Hi,
    I am using Android 2.1 update 1
    For AutoCompleteTextView in landscape orientation. I am not able to remove the completion hints on top of the Keyboard.
    I am using the following line of code

    nputMethodManager inputMethodManager =
    (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(tvSearchContentTextView.getWindowToken(),0);

    From the above lines the keyboard is removed but not able to remove the bar for completion hints.
    The user needs to press the back button of android phone.

    The activity is inside a Tab so, even if user clicks another tab nothing happens white bar is there untill user presses back button of android phone

    (I tested this on nexus one).

  6. hi,

    i want the soft keyboard to be displayed on the screen during the launch itself.\
    i have edit text and a button in my screen. I want the soft keyboard to be displayed on the screen without clicking the edit text . Can u please help me on this.

  7. Use the OnFocusListener of the Parent View/ViewGroup which holds the EditText and the Button as the place to invoke your IMM.

  8. And again do requestFocus on the same call back function to focus to the EditText so that the characters from teh keyboard are targetted to the EditText.

  9. Hello… I have tried this and it works. 😀
    I just have one question, is there a code for removing the chinese suggestions when the user enters something in the editText? It seems like this method does hide the keyboard but not the chinese suggesstions. 🙁

  10. it does work suggestion though don’t use editText as a variable name it’s confusing as it’s so similar to android EditText

  11. it by incorporating relevant ads through advertising…

    networks like google adsense or chitika. affiliate programs such as amazon also are becoming increasingly popular ways to earn extra revenue. in addition, you could sell private ads or review products on your blog.4. promote your blog by utilizing soci…

  12. because you may miss an important part…

    that will make things clearer for you later. make sure to follow the steps in the order presented.learning to write the english language as adults may feel overwhelming at first. i know it was overwhelming for me, but with practice you…

LEAVE A REPLY