Adding SearchView in Fragment

user264953 picture user264953 · Dec 15, 2015 · Viewed 42.9k times · Source

Trying to include a searchview in actionbar. For this, I have done the following:

Created MenuSearch.xml in the menu folder as given below:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
   <item
    android:id="@+id/action_search"
    android:actionViewClass="Android.Support.V7.Widget.SearchView"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="always"
    android:title="Search"/>

</menu>

Included the method OnCreateOptionsMenu in the fragment:

public override void OnCreateOptionsMenu(IMenu menu, MenuInflater inflater)
    {
        inflater.Inflate(Resource.Menu.MenuSearch, menu);
        base.OnCreateOptionsMenu(menu, inflater);
        var searchManager = (SearchManager) this.Activity.GetSystemService(Context.SearchService);
        searchView = (SearchView) (menu.FindItem(Resource.Id.action_search).ActionView);
        searchView.SetSearchableInfo(searchManager.GetSearchableInfo(Activity.ComponentName));

    }

And I have set HasOptionsMenu = true; in oncreate of the fragment.

With the code above, I get null pointer in the line searchView.SetSearchableInfo... . Not sure why this happens, though everything seem to be correct and in place.

Based on some suggestions for resolving similar issue, I have also tried out the below option for searchview.

        var item = menu.FindItem (Resource.Id.action_search);
        var searchItem = MenuItemCompat.GetActionView(item);
        searchView = searchItem.JavaCast<Android.Support.V7.Widget.SearchView>();
        searchView.SetSearchableInfo(searchManager.GetSearchableInfo(Activity.ComponentName));

But again, I get searchView as null.

Now that I have tried the possible options, I am clueless as to why searchview becomes null.

Any help is much appreciated.

Answer

Pavan Bilagi picture Pavan Bilagi · Dec 15, 2015

Change your MenuSearch.xml to

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item android:id="@+id/action_search"
            android:title="@string/app_name"
            android:icon="@drawable/ic_action_search"
            app:showAsAction="collapseActionView|ifRoom"
             />
    </menu>

And in your fragment, add searchView.setOnQueryTextListener

    @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            super.onCreateOptionsMenu(menu, inflater);
            menu.clear();
            inflater.inflate(R.menu.search_option_menu, menu);
            MenuItem item = menu.findItem(R.id.action_search);
            SearchView searchView = new SearchView(((MainActivity) mContext).getSupportActionBar().getThemedContext());
           // MenuItemCompat.setShowAsAction(item, //MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | //MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
           //  MenuItemCompat.setActionView(item, searchView);
           // These lines are deprecated in API 26 use instead
 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
        item.setActionView(searchView);
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }
                @Override
                public boolean onQueryTextChange(String newText) {
                    return false;
                }
            });
            searchView.setOnClickListener(new View.OnClickListener() {
                                              @Override
                                              public void onClick(View v) {

                                              }
                                          }
            );
        }