3

I have an ActionBar with a SearchView.

I want the search view to always be "open" (expanded). Doing that is possible using

mSearchView.setIconifiedByDefault(false);

But it for some reason moves the search icon outside of the text area: enter image description here

If I don't use setIconifiedByDefault(), it looks like I want it to look when it is expanded: enter image description here

But then it collapses into a search icon on back, and also

MenuItemCompat.expandActionView(searchItem);

Does not seem to help.

Is there a way to achieve what I want?

dors
  • 5,802
  • 8
  • 45
  • 71

3 Answers3

1

You can just intercept OnClose event:

searchView.setOnCloseListener(new SearchView.OnCloseListener() {
    @Override
    public boolean onClose() {
        return true;
    }
});
Anril
  • 66
  • 4
0

I was able to solve this issue by changing my XML from

<item android:id="@+id/search"
android:icon="@drawable/my_icon"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />

to

<item android:id="@+id/search"
android:icon="@drawable/my_icon"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView" />
Chris
  • 377
  • 2
  • 11
0

Always show/expand search box on an action bar

Its a 2 steps solution,

1.public void onCreateOptionsMenu(M... ... searchView.setIconifiedByDefault(false); }

2. In the menu xml file, make sure android:showAsAction="always" is set

<item    
        android:id="@+id/grid_default_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="search"
        android:showAsAction="always"
        android:actionViewClass="android.widget.SearchView"
    />
Aydın Ahmed
  • 559
  • 5
  • 11