androidactionbarsherlock

Showing the soft keyboard for SearchView on ActionBar


We've got a SearchView on the ActionBar which is set to be non-iconified. As we don't have any content in the view until the user's entered something to search for, it would make sense to give the SearchView initial focus, and make sure the soft keyboard is showing ready for the user to enter text — otherwise they'll always have to first tap in the SearchView.

I can give the SearchView focus by just calling

searchView.requestFocus();

but I can't get the soft keyboard to appear. In another one of our Fragments I have an EditText which we want to be focused I can get the soft keyboard to appear there by calling

InputMethodManager mgr = (InputMethodManager)getActivity().getSystemService(
            Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

but this just doesn't work on the SearchView. It must surely be possible to get this to work.


Solution

  • Further rummaging around StackOverflow and I found this question:

    Forcing the Soft Keyboard open

    which contains a solution that worked for me:

    ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).
        toggleSoftInput(InputMethodManager.SHOW_FORCED,
                        InputMethodManager.HIDE_IMPLICIT_ONLY);