Search code examples
androidsearchviewandroid-5.0-lollipopmaterial-design

SearchView not appearing as action view in android lollipop


I've been converting my app to the new material design standard and have been replacing action bar sherlock with actionbarcompat. Though I have come across a strange issue regarding the searchview widget in my application. The application is crashing because whenever I try to get the action view, the widget that is returned is null. This was determined after extensive debugging. When I commented out the code that was causign the crash, the search appears as an overflow item rather than as an action item even after being explicitly set to show always.

Here is the menu xml

<item
    android:id="@+id/search"
    app:actionViewClass="android.widget.SearchView"
    android:icon="@android:drawable/ic_menu_search"
    app:showAsAction="always|collapseActionView"
    android:showAsAction="always|collapseActionView"
    android:title="@string/title" />

And heres is the section of code which inflated the menu and attempts to get the action item. The line that is commented out is the one that is throwing and error and all the lines after that are dependant on that object.

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.homescreen, menu);
    MenuItem item=menu.findItem(R.id.search);
//SearchView sv = (SearchView) MenuItemCompat.getActionView(item);
//SearchManager manager = (SearchManager)getActivity().getSystemService(Context.SEARCH_SERVICE);
//sv.setSearchableInfo(manager.getSearchableInfo( newComponentName(getActivity().getApplicationContext(), SearchActivity.class)));
        sv.setOnQueryTextListener(this);
        sv.setIconifiedByDefault(false);

}

Thanks for any help!


Solution

  • change:

     app:actionViewClass="android.widget.SearchView"
    

    to

     app:actionViewClass="android.support.v7.widget.SearchView"