Using Search in App Bar to Query in a fragment

1.6k views Asked by At

So basically I'm developing an app with a navigation bar and tabbed layout with a search icon added in the app bar. each of the tabs has a listview. Please give me an Idea on how to use the search to query in a fragment like this.

enter image description here

1

There are 1 answers

0
gautam On

Try this

 @Override
    public void onPrepareOptionsMenu(Menu menu)
    {
        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager)getActivity(). getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
        {
            @Override
            public boolean onQueryTextSubmit(String query)
            {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String query)
            {
               // pass query to your filter 
                return true;
            }

        });
    }