Expand Search View to use entire Action Bar (hide other things)
Solution 1
Well you could imitate that yourself by hiding all the other items when the SearchView
is expanded:
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (android.widget.SearchView) searchItem.getActionView();
// Detect SearchView icon clicks
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setItemsVisibility(menu, searchItem, false);
}
});
// Detect SearchView close
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
setItemsVisibility(menu, searchItem, true);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {
for (int i=0; i<menu.size(); ++i) {
MenuItem item = menu.getItem(i);
if (item != exception) item.setVisible(visible);
}
}
Solution 2
This would be a late answer but you could add this attribute to your menu item and the work is done for you.
app:showAsAction="collapseActionView|always"
Keyword here being the collapseActionView.
Solution 3
I tried this line of code to make the searchview expand the full width available. This works when there are other items shown in the actionbar either.
searchView.setMaxWidth(android.R.attr.width);
Related videos on Youtube
user3672263
Updated on April 24, 2021Comments
-
user3672263 about 3 years
I have a
SearchView
inside myActionBar
, and I want to use the entireActionBar
when the search icon is pressed, but I can only use theActionBar
free spacemy menu code:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context="br.com.moderna.houaiss.activity.SearchActivity" > <item android:id="@+id/search" android:actionViewClass="android.widget.SearchView" android:icon="@drawable/ic_search_white_48dp" android:showAsAction="collapseActionView|always" android:title="@string/search"/> <item android:id="@+id/backWardHistory" android:icon="@drawable/ic_arrow_back_white_48dp" android:showAsAction="always" android:title="@string/back_history"/> <item android:id="@+id/forWardHistory" android:icon="@drawable/ic_arrow_forward_white_48dp" android:showAsAction="always" android:title="@string/forward_history"/> <item android:id="@+id/action_home" android:orderInCategory="100" android:showAsAction="never" android:title="@string/action_home"/> <item android:id="@+id/action_about" android:orderInCategory="100" android:showAsAction="never" android:title="@string/action_about"/> <item android:id="@+id/action_configuration" android:orderInCategory="100" android:showAsAction="never" android:title="@string/action_configuration"/> <item android:id="@+id/action_logout" android:orderInCategory="100" android:showAsAction="never" android:title="@string/action_logout"/>
I had tryed
android:showAsAction="ifRoom"
, but I need them to be always on myActionBar
, and not inside my menu.EDIT ------
Thanks to @Simas,
I used setOnActionExpandListener method to achieve this effect
final MenuItem searchItem = menu.findItem(R.id.search); searchItem.setOnActionExpandListener(new OnActionExpandListener() { @Override public boolean onMenuItemActionExpand(final MenuItem item) { SearchActivity.this.setItemsVisibility(menu, searchItem, false); return true; } @Override public boolean onMenuItemActionCollapse(final MenuItem item) { SearchActivity.this.setItemsVisibility(menu, searchItem, true); return true; } }); private void setItemsVisibility(final Menu menu, final MenuItem exception, final boolean visible) { for (int i = 0; i < menu.size(); ++i) { MenuItem item = menu.getItem(i); if (item != exception) item.setVisible(visible); } }
-
user3672263 almost 9 yearsthis helped me to achieve a solution
-
Sumit over 8 yearssearchView.setOnCloseListener not work so use MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener()....
-
Rishabh Bhardwaj about 8 yearsVery well defined .Thanks
-
Sagar Aghara about 7 years@Simas You saved my Day nd Time...Thanks for Amazing answer...+1 vote for your ans.
-
Zharf almost 7 yearsthis doesn't seem to work for me, only works for the search item that has the actionview
-
RadekJ over 4 yearsputting there
android.R.attr.width
is confusing, as it has anything to do with width or any value under that attribute, it just happens that resource id is a big integer number. it make more sense to wtire:searchView.setMaxWidth(Integer.MAX_VALUE)
-
Dasser Basyouni about 3 yearsfor me
setOnCloseListener
wasn't being called. solution >> stackoverflow.com/questions/13920960/… -
Kenneth Murerwa almost 3 yearsThis is the simplest and most direct solution. Thank you.