0

I'm trying to implement SearchView in standalone Toolbar in a fragment but unable to get any result.

I followed this answer SearchView at a secondary Toolbar but no success.

Here is my implementation

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_tab_fragment3, container, false);
        initViews(rootView);
        initBottomToolbar(rootView);
        return rootView;
    }

    private void initBottomToolbar(View rootView) {
        mToolbarBottom = (Toolbar) rootView.findViewById(R.id.toolbar_bottom);
        // Set an OnMenuItemClickListener to handle menu item clicks
        mToolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                // Handle the menu item
                Toast.makeText(getActivity(), "action_search clicked", Toast.LENGTH_SHORT).show();

                SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
                SearchView searchView = null;

                if (menuItem != null) {
                    Log.d("Bottom Toolbar", "menuItem != null");
                    searchView = (SearchView) menuItem.getActionView();
                }

                if (searchView != null) {
                    Log.d("Bottom Toolbar", "searchView != null");
                    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

                    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                        @Override
                        public boolean onQueryTextSubmit(String query) {
                            Toast.makeText(getActivity(), query, Toast.LENGTH_SHORT).show();
                            Log.e("Bottom Toolbar", "Submitted: "+query);
                            return false;
                        }

                        @Override
                        public boolean onQueryTextChange(String newQuery) {
                            Toast.makeText(getActivity(), newQuery, Toast.LENGTH_SHORT).show();
                            Log.d("Bottom Toolbar", "Changed: " + newQuery);
                            List<ContactsBean> filteredContactsBeanList = filter(mContactsBeanList, newQuery);
                            mContactsAdapter.animateTo(filteredContactsBeanList);
                            mContactsRecyclerView.scrollToPosition(0);
                            return true;
                        }
                    });
                }
                return true;
            }
        });

        // Inflate a menu to be displayed in the toolbar
        mToolbarBottom.inflateMenu(R.menu.menu_bottom_toolbar);

    }

Even, if I click SearchView, it's neither showing Toast nor showing any message in Log.

Community
  • 1
  • 1
ch3tanz
  • 3,070
  • 3
  • 16
  • 24
  • http://v4all123.blogspot.in/2015/08/simple-example-for-using-searchview-in.html may be this will help you. – Gunaseelan Sep 23 '15 at 06:31
  • @Gunaseelan This is for primary Toolbar. I've 2 Toolbars , One in activity and other one is in fragment. – ch3tanz Sep 23 '15 at 06:38

1 Answers1

0

I made it work by inflating first the Menu on my toolbarBottom before binding the listeners

toolbarBottom.inflateMenu(R.menu.menu_contacts_bottom);
    if(toolbarBottom.getMenu().size() > 0){
        SearchManager searchManager = (SearchManager) getActivity().getSystemService(
            Context.SEARCH_SERVICE);
        MenuItem searchMenuItem = toolbarBottom.getMenu().getItem(toolbarBottom.getMenu().size()-2);
        mSearchView = (SearchView) searchMenuItem.getActionView();
        if (mSearchView != null) {
            mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        }

        mSearchView.setOnCloseListener(new OnCloseListener() {
            @Override
            public boolean onClose() {
                Log.d(TAG, "onClose");
                return false;
            }
        });

        mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                Log.d(TAG, "query: " + newText);
                return false;
            }
        });
    }
Lester
  • 283
  • 7
  • 14