I have a Search icon in my toolbar that searches a RecyclerView list.
Pic 1:
When the icon is clicked, the toolbar shows the SearchView's setQueryHint " Search here...".
Pic 2:
When a character is entered on the SearchView line, in this case the number "4", it is then searched in the RecyclerView list. If the character is not found, a close button "x" appears to the right of the character:
Pic 3:
My problem is that the default behavior when clicking on the close button "x" is to close the SearchView which returns the view back to Pic 1, the initial icon View. I would like to have the View return to the SearchView line "Search here...", which is Pic 2. I try to use the setQueryHint on the SearchView in the mCloseButton.OnClickListener but it doesn't work. See the ** below in the mCloseButton OnClickListener() for the other snippets I tried that did not work. What am I missing here?
menu.xml
<item android:id="@+id/action_search"
android:title="@string/search_title"
android:icon="@drawable/ic_action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView" />
Activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.cardview_menu, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
if (adapter == null || adapter.getItemCount() == 0) {
searchItem.setVisible(false);
} else {
searchItem.setVisible(true);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
final EditText mSearchEditText = (EditText)mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
final ImageView mCloseButton = (ImageView) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
mSearchView.setQueryHint(" Search here...");
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true; // Return true to expand action view
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
adapter.clear();
adapter.addAll(allList);
return true; // Return true to collapse action view
}
});
mCloseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Clear the query
mSearchView.setQuery("",false);
// below lines were all tried and do not work
**mSearchView.setQueryHint(" Search here...");**
**mSearchView.setIconified(false);**
**mSearchView.onActionViewCollapsed);**
**MenuItemCompat.expandActionView(searchitem);**
adapter.clear();
adapter.addAll(allList);
}
});
...
return super.onCreateOptionsMenu(menu);
}