4

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>
Earwin delos Santos
  • 2,965
  • 7
  • 20
  • 29

3 Answers3

3

You need to change the value of android:showAsAction to always. The SearchView's attribute android:iconifiedByDefault should be true.

dieter_h
  • 2,707
  • 1
  • 13
  • 19
3

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.

Gober
  • 3,632
  • 3
  • 26
  • 33
2

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);
manish poddar
  • 474
  • 3
  • 17