Auto Collapse ActionBar SearchView on Soft Keyboard close

AndroidAndroid ActionbarAndroid Softkeyboard

Android Problem Overview


I am currently using an ActionBar menu item to display a SearchView in the action bar. When the search menu item is expanded the soft keyboard is displayed which is what I want. Now, when the user presses the back button to close the soft keyboard, I would also like to collapse the SearchView in the action bar.

I have tried implementing the following listeners OnKeyListener and OnFocusChangeListener on the MenuItem and the ActionView. I have also tried using OnBackPressed() in the Activity. None of the above detect when the back button is used to close the soft keyboard.

Any ideas?

I have implemented OnActionExpandListener to know when the SearchView is visible.

Android Solutions


Solution 1 - Android

I'll expand on @user1258568 's answer for the lazy. This worked for me. Note that it clears your query when focus is lost.

final MenuItem searchMenuItem = optionsMenu.findItem(R.id.search);
final SearchView searchView = (SearchView) searchMenuItem.getActionView();

searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean queryTextFocused) {
        if(!queryTextFocused) {
            searchMenuItem.collapseActionView();
            searchView.setQuery("", false);
        }
    }
});

Solution 2 - Android

I found a better solution.

searchView.setOnQueryTextFocusChangeListener(). 

The OnQueryTextFocusChangeListener gets called when the keyboard is displayed or hidden. Gets called first when the keyboard is displayed and the search view will have focus. Gets called again when keyboard is hidden and search view will lose focus, can close search view then using

menuItem.collapseActionView().

Solution 3 - Android

Just Override onBackPressed like this:

@Override
    public void onBackPressed() {
        if (searchView.isShown()){
            searchView.onActionViewCollapsed();  //collapse your ActionView
            searchView.setQuery("",false);       //clears your query without submit
            isClosed = true;                     //needed to handle closed by back
        } else{
            super.onBackPressed();
        }
    }

and your onCreateOptionsMenu would inflate the mSearchView like this:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_search, menu);
        mSearchView = (SearchView) menu.findItem(R.id.menu_action_search).getActionView();
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setOnSearchClickListener(this);
        mSearchView.setOnCloseListener(this);
        isClosed = true;
        return true;
    }

have you class implement the following like this:

public class myActivity extends FragmentActivity implements
    SearchView.OnQueryTextListener, View.OnClickListener, SearchView.OnCloseListener {

which you will also need:

@Override
public void onClick(View view) {
    isClosed = false;
}

@Override
public boolean onClose() {
    isClosed = true;
    return false;
}

You will need to make "mSearchView" and "isClosed" both global variables to the activity.

Solution 4 - Android

The answer from Jon Willis works great. This is an improvement to his answer.

First, create a new class that implements View.OnFocusChangeListener:

public class SearchViewFocusListener implements View.OnFocusChangeListener {

    private final MenuItem mMenuItem;

    public SearchViewFocusListener(MenuItem menuItem) {
        mMenuItem = menuItem;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            mMenuItem.collapseActionView();
            if (v instanceof SearchView) {
                ((SearchView) v).setQuery("", false);
            }
        }
    }

}

Next, set the listener on your SearchView:

searchView.setOnQueryTextFocusChangeListener(new SearchViewFocusListener(menuItem));

Solution 5 - Android

You only need to put the "collapseActionView" attribute in the menu layout

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_item_search"
        android:title="@string/search"
        android:iconifiedByDefault="true"
        android:icon="@drawable/ic_action_search" 
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView"/> <--this one
</menu>

That will give you the functionality you look for all by itself.Don't forget to call the method "clearFocus" on the SearchView to close the keyboard once you send the query.

Solution 6 - Android

This is what I did for making the keyboard disappear. You can try to see if this works for you. I set the searchView to invisible and then to visible again.

	//set query change listener
     searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
		@Override
		public boolean onQueryTextChange(String newText) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean onQueryTextSubmit(String query) {
			/**
			 * hides and then unhides search tab to make sure keyboard disappears when query is submitted
			 */
				  searchView.setVisibility(View.INVISIBLE);
				  searchView.setVisibility(View.VISIBLE);
			return false;
		}
    	 
     });

Solution 7 - Android

It's achievable like this:

   private void setupSearchView(Menu menu) {
        final MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        final SearchView searchView = (SearchView) searchMenuItem.getActionView();
        
        [...]

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                searchMenuItem.collapseActionView();
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                return true;
            }
        });
    }

Solutions based on setOnQueryTextFocusChangeListener() did not work for me because the event was not launched - the searchView did not lose focus when submitted, probably because I perform the search in the same activity that contains the Search View.

Anyway, I think using OnQueryTextListener is more correct, as it describes the event of submitting text more precisely.

Solution 8 - Android

@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.home_screen, menu);

		SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
		final MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
		final SearchView searchView = (SearchView) searchMenuItem
				.getActionView();
		searchView.setIconifiedByDefault(false);
		if (searchManager != null && searchView != null) {
			searchView.setSearchableInfo(searchManager
					.getSearchableInfo(getComponentName()));

			searchView
					.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

						@Override
						public void onFocusChange(View v, boolean hasFocus) {

							if (!hasFocus) {
								if (searchMenuItem != null) {
									searchMenuItem.collapseActionView();
								}// end if
								if (searchView != null) {
									searchView.setQuery("", false);

								}// end if
							}// end if

						}
					});

			searchView
					.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

						@Override
						public boolean onQueryTextSubmit(String query) {
							/**
							 * hides and then unhides search tab to make sure
							 * keyboard disappears when query is submitted
							 */
							if (searchView != null) {
								searchView.setVisibility(View.INVISIBLE);
								searchView.setVisibility(View.VISIBLE);

							}
							return false;
						}

						@Override
						public boolean onQueryTextChange(String newText) {
							// TODO Auto-generated method stub
							return false;
						}
					});

		}

		return super.onCreateOptionsMenu(menu);
	}

Solution 9 - Android

If you want to collapse keyboard when user clicks search icon on keyboard this can be achieved by simple

inside onquerytextsubmitted {

searchView.clearfocus()

}

Solution 10 - Android

You need to call setIconified twice.

To actually collapse your search view and close the keyboard.
With first call text of search view is cleared with second call keyboard and search view get closed.

Solution 11 - Android

For some reason, menuItem.collapseActionView() did not work so I used searchView.setIconified(true) instead.

This gives the below result as the code sample.

final MenuItem searchItem = (MenuItem) menu.findItem(R.id.menu_item_search);
final SearchView searchView = (SearchView) searchItem.getActionView();

searchView.setOnQueryTextFocusChangeListener(new SearchView.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            searchView.setIconified(true);
        }
    }
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1258568View Question on Stackoverflow
Solution 1 - AndroidJon WillisView Answer on Stackoverflow
Solution 2 - Androiduser1258568View Answer on Stackoverflow
Solution 3 - AndroidCodeversedView Answer on Stackoverflow
Solution 4 - AndroidJared RummlerView Answer on Stackoverflow
Solution 5 - AndroidRodrigo de BlasView Answer on Stackoverflow
Solution 6 - AndroidParnitView Answer on Stackoverflow
Solution 7 - AndroidGaRRaPeTaView Answer on Stackoverflow
Solution 8 - AndroidconfuciusView Answer on Stackoverflow
Solution 9 - AndroidgeniushkgView Answer on Stackoverflow
Solution 10 - Androidnosaiba darwishView Answer on Stackoverflow
Solution 11 - AndroidbastienView Answer on Stackoverflow