Andrew Andrew - 9 months ago 67
Android Question

Close virtual keyboard on button press

I have an

with an
, a button and a
. The purpose is to type a search screen in the
, press the button and have the search results populate this list.

This is all working perfectly, but the virtual keyboard is behaving strange.

If I click the
, I get the virtual keyboard. If I click the "Done" button on the virtual keyboard, it goes away. However, if I click my search button before clicking "Done" on the virtual keyboard, the virtual keyboard stays and I can't get rid of it. Clicking the "Done" button does not close the keyboard. It changes the "Done" button from "Done" to an arrow and remains visible.

Thanks for your help

Answer Source
mMyTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            // hide virtual keyboard
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            return true;
        return false;