Peri Hartman Peri Hartman - 7 months ago 45
Android Question

SearchView in OptionsMenu not full width

I have a working SearchView which expands in my OptionsMenu when the user taps on the search icon. However it only expands within the available space among the other OptionsMenu icons. On a wide screen this is fine, but with a narrow space there is only room to show 5-10 charaters in the search box. I want it to overlay the other icons such as it does for the Android Contacts app. Currently, I'm building with targetSdkVersion = 17. Hopefully I'm missing something simple :)

(Note added later: the only solution I've found workable so far is to hide all the menu icons when I want to expand the search icon. This is conceptually simple. But it is messy because when restoring hidden icons, one has to go through a bunch of logic to figure out which ones to restore, or keep state variables around, etc.)

Here's my item xml in for the OptionsMenu:

android:title="Search Current Folder"
android:actionViewClass="android.widget.SearchView" />

I also have in my main activity code:

public boolean onCreateOptionsMenu (Menu menu)
getMenuInflater().inflate(, menu);
this.optionsMenu = menu;

MenuItem searchItem = menu.findItem (;
searchItem.setOnActionExpandListener (this);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint (getString (R.string.search_shallow_hint));

searchItem = menu.findItem (;
searchItem.setOnActionExpandListener (this);
searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint (getString (R.string.search_deep_hint));


public boolean onMenuItemActionExpand(MenuItem item)
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener (this);
return true;

public boolean onMenuItemActionCollapse(MenuItem item)
SearchView searchView = (SearchView) item.getActionView();
searchView.setQuery ("", false);
return true;


I had a similar issue, that the search bar did not fill the whole width,( but I had no other icons). I solved it by adding in item:


and in layout/my_search_view.xml :

<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android=""
    android:layout_height="wrap_content" />