@Override
 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
   if (keyCode == KeyEvent.KEYCODE_BACK) {
     // special case for the back key, we do not even try to send it
     // to the drop down list but instead, consume it immediately
     if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
       KeyEvent.DispatcherState state = getKeyDispatcherState();
       if (state != null) {
         state.startTracking(event, this);
       }
       return true;
     } else if (event.getAction() == KeyEvent.ACTION_UP) {
       KeyEvent.DispatcherState state = getKeyDispatcherState();
       if (state != null) {
         state.handleUpEvent(event);
       }
       if (event.isTracking() && !event.isCanceled()) {
         mSearchView.clearFocus();
         mSearchView.setImeVisibility(false);
         return true;
       }
     }
   }
   return super.onKeyPreIme(keyCode, event);
 }
    /**
     * We override this method to be sure and show the soft keyboard if appropriate when the
     * TextView has focus.
     */
    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
      super.onWindowFocusChanged(hasWindowFocus);

      if (hasWindowFocus && mSearchView.hasFocus() && getVisibility() == VISIBLE) {
        InputMethodManager inputManager =
            (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(this, 0);
        // If in landscape mode, then make sure that
        // the ime is in front of the dropdown.
        if (isLandscapeMode(getContext())) {
          ensureImeVisible(true);
        }
      }
    }
 @Override
 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
   super.onFocusChanged(focused, direction, previouslyFocusedRect);
   mSearchView.onTextFocusChanged();
 }