Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

<item   android:id="@+id/menu_search_shallow"   android:title="Search Current Folder"   android:icon="@drawable/ic_btn_search"   android:showAsAction="always|collapseActionView"   android:actionViewClass="android.widget.SearchView" /> 

I also have in my main activity code:

@Override public boolean onCreateOptionsMenu (Menu menu) {   getMenuInflater().inflate(R.menu.nav_menu, menu);   this.optionsMenu = menu;    MenuItem searchItem = menu.findItem (R.id.menu_search_shallow);   searchItem.setOnActionExpandListener (this);   SearchView searchView = (SearchView) searchItem.getActionView();   searchView.setQueryHint (getString (R.string.search_shallow_hint));    searchItem = menu.findItem (R.id.menu_search_deep);   searchItem.setOnActionExpandListener (this);   searchView = (SearchView) searchItem.getActionView();   searchView.setQueryHint (getString (R.string.search_deep_hint)); } 

and

@Override public boolean onMenuItemActionExpand(MenuItem item)  {   SearchView searchView = (SearchView) item.getActionView();   searchView.setOnQueryTextListener (this);   return true; }  @Override public boolean onMenuItemActionCollapse(MenuItem item)  {   SearchView searchView = (SearchView) item.getActionView();   searchView.setQuery ("", false);   return true; } 
like image 748
Peri Hartman Avatar asked Aug 05 '13 16:08

Peri Hartman


People also ask

How do I expand SearchView?

To make the SearchView expanded by default, call setIconifiedByDefault(false) on it when you initialise it (e.g. in onCreateOptionsMenu(..) or onPrepareOptionsMenu(..) ). I've found in most cases this will give it focus automatically, but if not simply call requestFocus() on it too.

How do I use search view?

Android SearchView provides user interface to search query submitted over search provider. SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class.


2 Answers

Instead of creating a new layout I set the MaxWidth programmatically in onCreateOptionsMenu():

@Override public boolean onCreateOptionsMenu(Menu menu) {     MenuInflater inflater = getMenuInflater();     inflater.inflate(R.menu.menu_search, menu);     SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();     searchView.setMaxWidth(Integer.MAX_VALUE);     .... 
like image 177
shoke Avatar answered Oct 12 '22 14:10

shoke


For some reason jjung's solution didn't work for me. I had to add android:maxWidth="10000dp", just a really high maxWidth, to get it to expand to the full available width.

Also, just to add, I'm using the support library (v7), and so my layout file looks like this:

<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:maxWidth="10000dp" /> 

And in my item, I have:

<menu xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:myapp="http://schemas.android.com/apk/res-auto">     <item         ...         myapp:actionLayout="@layout/searchview" /> ... 

Where myapp is just some arbitrary namespace for the attributes added by the support library (see Adding Action Items for more details).

like image 22
mchristie Avatar answered Oct 12 '22 14:10

mchristie