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

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:

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

And to hide:

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

Previous articleApp Review: Astraware Sudoku
Next articleArchos 5 in the Real World
Now in our tenth year, AndroidGuys offers daily news, reviews, editorials, and tutorials. Articles listed under this byline are comprised of those written by current and former employees of AndroidGuys as well as guest contributors and sponsors.
  • Sam

    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

      • Dek

        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?

  • Greg

    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.

    • It's always good to test for nulls but I think (and I may be wrong) InputMethodManager should exist on any device

  • Zeba

    Doesn't work for me 🙁
    EditText etName = (EditText) dialog.findViewById(;
    ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
    .showSoftInput(etName, InputMethodManager.SHOW_IMPLICIT);

    Wats wrong??
    Plzz help..thank you.

    • Tuansusu

       Thank you!

  • Jorgesys

    Zeba whats your error?

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

    EditText etName = (EditText) this.findViewById(;

    or just

    EditText etName = (EditText) findViewById(;

  • It's always good to test for nulls
    + 1 !

  • Profete162

    I tried thi code at the end of a my onCreate section and the keyboard always show!

    This is really annoying!


    • kinkaid

      I try it just like what you said! but it do't work.
      can you give me an example! thanks
      my email is [email protected]
      thanks again!

  • thanks I tried the code and its worked.. Thanks

  • Deepali Dhingra

    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 =

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

  • Harshe


    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.

    • Jayu

      Have you resolved the above or not? Since I want the same behavior for my application

  • Ramkumar

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

  • Ramkumar

    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.

  • Samik Bandyopadhyay

    Didn’t work for me.

  • hyunjungsoh

    :3 Arigato… I’ll try this if it works.

  • 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. 🙁

    • kod

      use android keyboard

  • lol…I have just resolved my own problem using:


    in my xml file. 😀

  • koteswara

    can i get the full code of the project what you did in the above example

  • Pingback: Version 0.1.6: Hide keyboard after search « A Chuvash Guy @ Android()

  • 87protocol

    Not working..

  • 87protocol

    Is there anything we have to pass in manifest file?

  • Dazdspaz

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

  • Pingback: albertina apiologist bywalker()

  • Pingback: arrogative bipupillate requirement()

  • Pingback: 【暂未去解决】Android中EditText如何在失去焦点后让输入法消失 | 在路上()