December 20, 2014

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]



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

    • http://intensedebate.com/people/droidin droidin

      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.

    • http://intensedebate.com/people/droidin droidin

      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 :(
    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.

    • 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(R.id.txtName);

    or just

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

  • http://keyboardpad.com Keyboard pad man

    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!

    Profete162

  • https://mutiarar06.student.ipb.ac.id Mutia

    thanks I tried the code and its worked.. Thanks

  • Deepali Dhingra

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

  • Harshe

    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

      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.

  • http://www.breisa.deviantart.com breisa

    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

      use android keyboard

  • http://www.breisa.deviantart.com breisa

    lol…I have just resolved my own problem using:

    android:inputType=”textVisiblePassword”

    in my xml file. :D

  • 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如何在失去焦点后让输入法消失 | 在路上