How To Dismiss/close/collapse Searchview In Actionbar In Mainactivity?
Solution 1:
Use:
searchMenuItem.collapseActionView();
Instead of:
searchView.onActionViewCollapsed();
If you are using AppCompat library
, then use:
MenuItemCompat.collapseActionView(searchMenuItem);
Solution 2:
In my case calling invalidateOptionsMenu();
closes SearchView
Solution 3:
Solution mentioned here is simple and works perfectly.
Basically, Call setQuery("", false)
and setIconified(true)
on SearchView
.
Solution 4:
In menu_main.xml add :
<item android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="Search"
app:showAsAction="collapseActionView|ifRoom"
android:orderInCategory="1"
app:actionViewClass="android.support.v7.widget.SearchView"
android:menuCategory="secondary"
/>
and in onCreateOptionsMenu
final MenuItem miSearch = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) miSearch.getActionView();
searchView.setQueryHint("Searh For");
searchView.setOnQueryTextListener(newSearchView.OnQueryTextListener() {
@OverridepublicbooleanonQueryTextSubmit(String query) {
Toast.makeText(context,query, Toast.LENGTH_LONG).show();
// Here Put Your Code.//searchView.onActionViewCollapsed();
miSearch.collapseActionView();
returnfalse;
}
@OverridepublicbooleanonQueryTextChange(String newText) {
returnfalse;
}
});
Solution 5:
It sounds like what you are looking for is a way to close the search bar programmatically. Unfortunately, there is not a method or an equivalent workaround for this. You may have already seen another post that had some suggestions, but no real way to do this.
Your first edit above that calls setIconified(true)
is the best alternative. The docs suggest calling setIconified(true)
should collapse the search widget, clear it, and remove it from focus if the attribute iconifiedByDefault
is true (which it is by default so I don't see any problem with your activity_main_actions.xml).
Post a Comment for "How To Dismiss/close/collapse Searchview In Actionbar In Mainactivity?"