I have set up a Toolbar
and within, a SearchView
widget. The XML of this Toolbar
's menu is the following:
<item android:id="@+id/searchView"
android:title="@string/search_title"
android:icon="@drawable/ic_baseline_search_24px"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom" />
On the fragment which has this Toolbar
, I have overrided its method onCreateOptionsMenu
to display the Toolbar
and to configure SearchView
.
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menu_inflater) {
menu_inflater.inflate(R.menu.action_bar_menu, menu);
super.onCreateOptionsMenu(menu,menu_inflater);
AppCompatActivity app_compat_activity = (AppCompatActivity) getActivity();
SearchView search_view = (SearchView) menu.findItem(R.id.searchView).getActionView();
search_view.setIconified(false);
search_view.setIconifiedByDefault(false);
search_view.clearFocus();
SearchManager search_manager = (SearchManager) Objects.requireNonNull(app_compat_activity).getSystemService(Context.SEARCH_SERVICE);
search_view.setSearchableInfo(Objects.requireNonNull(search_manager).getSearchableInfo(app_compat_activity.getComponentName()));
}
My aim is that the SearchView
expands (filling all the Toolbar
's width). The above code doesn't work.
Change
app:showAsAction="collapseActionView|ifRoom"
to
app:showAsAction="always"
Remove search_view.clearFocus();