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.