Reputation: 3065
I already define setIconified()
and setIconifiedByDefault()
to false but the SearchView menu item is not expanding by default. Here's how I implemented it:
View customTitle = getLayoutInflater().inflate(R.layout.toolbar_custom_title, null);
toolbar.inflateMenu(R.menu.menu_buddies);
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
SearchView searchView = (SearchView) toolbar.getMenu().findItem(R.id.menu_search).getActionView();
searchView.setQueryHint("Search Buddies");
searchView.setIconified(false);
searchView.setIconifiedByDefault(false);
My menu_buddies:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_search"
xmlns:pawesome="http://schemas.android.com/apk/res-auto"
android:title="@string/action_search"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
pawesome:showAsAction="always|collapseActionView"
pawesome:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Upvotes: 4
Views: 10566
Reputation: 55
I think you solve your issue, but here the recommendation for others Just remove from menu_buddies i.e your menu item
pawesome:showAsAction="always|collapseActionView"
to this
pawesome:showAsAction="always"
cause collapseActionView make your searchview collapse, so again here the compelete menu item code ie. you menu_buddies
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_search"
xmlns:pawesome="http://schemas.android.com/apk/res-auto"
android:title="@string/action_search"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
pawesome:showAsAction="always"
pawesome:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Note: To make your searchView item from toolbar(either material toolbar or actionview) always expand make sure
In your menu item of searchview set
app:showAsAction="always"
SearchView searchView = (SearchView) toolbar.getMenu().findItem(R.id.menu_search).getActionView(); searchView.setIconified(false); searchView.setIconifiedByDefault(false);
make sure setIconified and setIconifiedByDefault false that's it
Upvotes: 3
Reputation: 484
None of the above answers worked for me.
Use this
SearchManager searchManager = (SearchManager) mActivity.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = new SearchView(mActivity.actionBar.getThemedContext());
searchView.setSearchableInfo(searchManager.getSearchableInfo(mActivity.getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setQueryHint("search");
menu.findItem(R.id.action_search).setActionView(searchView);
Upvotes: 2
Reputation: 3662
Have you solved this yet? Remove:
searchView.setIconified(false);
and keep only this line:
searchView.setIconifiedByDefault(false);
As well as showAsAction="always" in menu-xml.
This works for me, the searchview is expanded and can not be collapsed by any means.
Upvotes: 3
Reputation: 2727
You need to change the value of android:showAsAction
to always
. The SearchView's attribute android:iconifiedByDefault should
be true
.
Upvotes: 3