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:

[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]

About author

AndroidGuys
AndroidGuys 4627 posts

Founded on November 5, 2007, we've enjoyed bringing you the latest in Android news and rumors. Updated daily, we strive to deliver reviews, opinions, and updates on all things related to Android.

You might also like

News and Rumors

Samsung patent for ‘sports glasses’ uncovered

A recently approved design patent from South Korea gives us a glimpse into the potential future of Samsung’s next wearable tech product. As we’ve previously heard, Samsung figures to be

News and Rumors

So Just What Is The Ideal Refund Window for the Android Market? [VOTE NOW]

Since last Friday’s announcement of an overhauled Android Market, we’ve heard (and seen) plenty of complaints about the new return policy. Many of you feel 15 minutes is simply not long enough to download an app/game and decide whether or not it’s worth keeping. So, what is the ideal return window for the Android Market? If not 15 minutes, then what? VOTE IN OUR POLL.

News and Rumors

Search in private on your Android with Disconnect Search [App of the Day]

Sometimes it’s nice to be off the grid for awhile. What if you need to look something up, but don’t want it to be recorded everywhere? Now there is an

30 Comments

  1. Sam
    December 01, 18:15 Reply

    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.

    • droidin
      December 17, 21:31 Reply

      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
        January 14, 23:26 Reply

        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. Greg
    December 07, 15:42 Reply

    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.

    • droidin
      December 17, 21:25 Reply

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

  3. Zeba
    January 22, 07:24 Reply

    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. Jorgesys
    January 26, 22:09 Reply

    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. Profete162
    March 20, 11:19 Reply

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

    This is really annoying!

    Profete162

  6. Mutia
    June 15, 05:04 Reply

    thanks I tried the code and its worked.. Thanks

  7. Deepali Dhingra
    June 25, 07:25 Reply

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

  8. Harshe
    July 22, 09:06 Reply

    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.

    • Jayu
      October 12, 07:43 Reply

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

  9. Ramkumar
    August 04, 06:49 Reply

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

  10. Ramkumar
    August 04, 06:51 Reply

    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.

  11. breisa
    January 01, 06:00 Reply

    Hello… I have tried this and it works. :D
    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
      February 20, 01:20 Reply

      use android keyboard

  12. breisa
    January 01, 06:43 Reply

    lol…I have just resolved my own problem using:

    android:inputType=”textVisiblePassword”

    in my xml file. :D

  13. koteswara
    January 13, 07:17 Reply

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

  14. 87protocol
    January 11, 05:19 Reply

    Is there anything we have to pass in manifest file?

  15. Dazdspaz
    May 01, 06:10 Reply

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

Leave a Reply