Unable to show keyboard automatically in the SearchView

artem picture artem · Aug 19, 2012 · Viewed 20.4k times · Source

The SearchView is focused by default, but when I try to show software keyboard - it doesn't happen:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

But when I click on the SearchView - it does. Why?

Answer

artem picture artem · Aug 20, 2012

Fixed!

mSearchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {
                        @Override
                        public void onFocusChange(View view, boolean hasFocus) {
                            if (hasFocus) {
                                showInputMethod(view.findFocus());
                            }
                        }
                    });

And

private void showInputMethod(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
          imm.showSoftInput(view, 0);
        }
    }