SearchView focused when hiding the ActionBar menu

I have an ActionBar with a SearchView. The hidden/overflow menu items are shown on the right in a drop-down list when the menu button is selected. When the drop-down menu is hidden, the SearchView is focused and the keyboard shows. Is there a way to stop the keyboard from showing (except for the case where the SearchView is touched)?


  • Viewing private files created by an android app
  • Eclipse DDMS not showing Emulator on restart
  • Scale text in a view to fit?
  • Keep GridView when resuming from sleep mode
  • Android M Permissions: onRequestPermissionsResult() not being called
  • ExpandableListView - group indication is stretch to fit the text size
  • Edit added code below:
    Here is how I initialise it:

            SearchManager searchManager = (SearchManager) mActivity.getSystemService(Context.SEARCH_SERVICE);
            ((SearchView) mSearchView).setSearchableInfo(searchManager.getSearchableInfo(mActivity.getComponentName()));
                mSearchView.setQuery(mQuery, false);

    Edit 2:

    Here is what I do when the user wants to start the search:

        public boolean onQueryTextSubmit(String query) {
            // Hide keyboard
            InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(
            imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);

            return true;

    Related posts:

    Android: How to handle right to left swipe gestures
    How to draw a circle with animation in android with circle size based on a value
    How to Include additional javax.* packages in Android App with Eclipse?
    How to tell if 'Mobile Network Data' is enabled or disabled (even when connected b...
    java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/animation/AnimatorCompatHe...
    Support library: FragmentTransaction animations not working
  • Simple way to do dynamic but square layout
  • Android: Put GPSTimeStamp into jpg EXIF tags
  • Does the Android OS release a wakelock if the app or service holding it is killed?
  • Android: Sending a byte array via Http POST
  • Missing file even though project cleaned
  • Is the onUpgrade method ever called?
  • 6 Solutions collect form web for “SearchView focused when hiding the ActionBar menu”

    try call setFocusable(false) on SearchView when you init it.

    try calling mSearchView.clearFocus() on your initialization of the searchView. That worked for me when I had a similar problem.

    I instantiated a search view when creating the options menu in a fragment and none of the above methods worked for me.

    However, I was able to solve the problem by holding a reference to the SearchView, NOT setting the SearchView’s iconified setting in onCreateOptionsMenu(), but setting it on a post when the fragment resumed like follows:

    public void onResume() {
        getView().post(new Runnable() {
            public void run() {
                if (mFilterView.isIconfiedByDefault()) {

    Call requestFocus() for another view, ideally your results view, e.g.


    I have just been pulling my hair out over the same issue.

    I tried android:focusable=”false”. I tried the InputMethodManager. I tried creating a dummy view to take the focus but nothing worked.

    I finally solved it with the following code inside onCreateOptionsMenu():

    final ViewTreeObserver observer = mSearchView.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
        public void onGlobalLayout() {

    The issue was that at every point in the Fragment’s lifecycle the menu had not been created yet. The onCreateOptionsMenu() method was being called after onResume(), but only on some devices I tested on, which is really strange.

    The above code makes sure that clearing the focus is done after any focus changes during creation of the page.

    I hope this helps someone else, it would have saved me a lot of time to know this from the start.

    When you set up your SearchView in your XML layout file (if you’re using one), just use this:


    With this approach, your SearchView won’t gain focus until you touch it…no matter where or if you “init” it in your code, or you hide your menu, or any other activity that might not have occurred in your debugging. This also eliminates the possible need to track where you are calling

    setFocusable(false) //essentially does the same thing

    in your code multiple times as the accepted answer suggests.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.