3

I have a SearchView in my layout (not in the action bar) and I'm unable to dismiss the keyboard using the usual method:

public static void hideKeyboard(Activity activity) {
    InputMethodManager imm = (InputMethodManager)activity
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    if (imm != null && activity != null) {
        View currentFocus = activity.getCurrentFocus();

        if (currentFocus != null) {
            imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
        }
    }
}

The SearchView maintains its focus state. Using this method on older devices shows the keyboard being dismissed then redisplaying.

I believe the issue is with the fact that a SearchView is actually a hierarchy of Views that maintain their own state internally.

How do I dismiss the keyboard and unfocus the SearchView?

Brad
  • 9,113
  • 10
  • 44
  • 68

1 Answers1

8

It sounds like what might be happening is that you're unfocusing the SearchView and then it's saying "oh wait, I still have focus, I need the keyboard". Does activity.getCurrentFocus().clearFocus() clear out the focus?

Jonathan
  • 3,369
  • 4
  • 22
  • 27
  • In the source code you've posted, it doesn't look like you're manually calling clearFocus(). When I took a look at the source for SearchView, it looks like the call to setImeVisibility (which is what you want to modify here) and clearFocus are two separate ones, which means it might be helpful to manually clear out the focus yourself. Barring that, [this user](http://stackoverflow.com/questions/7409288/how-to-dismiss-keyboard-in-android-honeycomb-searchview) had a similar problem, although all the proposed solutions would require you to lose the generic interface that you currently expose. – Jonathan Jul 15 '13 at 03:46
  • 1
    You sir are a champion. I've completely misinterpreted the way that SearchView works. Thanks for the help. – Brad Jul 15 '13 at 04:01
  • 1
    Absolutely. For what it's worth it actually looks like you were pretty spot on about how SearchView works (there is an internal state). It looks like you just had your wires crossed about the difference between focus being requested and the keyboard being shown. – Jonathan Jul 15 '13 at 05:08