I have a "ProfileBaseActivity" what contains fragments. The activity has a custom Toolbar in the xml:
...
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:elevation="0dp"
android:minHeight="?attr/actionBarSize"
....
/>
...
I set my Toolbar in the activity, for each fragment using Java:
private void setupToolbar() {
toolbar.getMenu().clear();
toolbar.inflateMenu(R.menu.top_navigation);
...
}
I have the same toolbar in 3 fragment but my 4th fragment contains a RecyclerView list.. and i would like to change my toolbar in that fragment to sort the list with SearchView, so i created a menu xml:
<?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_view"
android:title="Search"
android:icon="@drawable/toolbar_serach_icon"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
/>
</menu>
If the user goes to "Search" fragment the "ProfileBaseActivity" only clears the toolbar's menu like toolbar.getMenu().clear();
In my "Search" fragment i am trying to set up my toolbar. I've tried two different ways:
1st
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_search, container, false);
recyclerView = view.findViewById(R.id.companies_rec_view);
adapter = new CompanyListRecyclerViewAdapter(getContext(), companies);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
setHasOptionsMenu(true);
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.toolbar_serach, menu);
MenuItem menuItem = menu.findItem(R.id.action_search_view);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
This solution not working, because the SearchView is not shown in the toolbar.
2nd
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_search, container, false);
toolbar = getActivity().findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.toolbar_serach);
MenuItem menuItem = toolbar.getMenu().findItem(R.id.action_search_view);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(this);
recyclerView = view.findViewById(R.id.companies_rec_view);
adapter = new CompanyListRecyclerViewAdapter(getContext(), companies);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
return view;
}
This version is a bit better, because the SearchView shown in the Toolbar, but unfortunately it is throwing a NullPointerException
at line SearchView searchView = (SearchView) menuItem.getActionView();
Of course i have implemented the SearchView within my Fragment like SearchFragment extends Fragment implements SearchView.OnQueryTextListener
and i have overwritten the two methods came from the interface:
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
List<Company> companiesNewList = new ArrayList<>();
for(Company company : companies){
if(company.getName().toLowerCase().equals(s.toLowerCase()) || company.getCity().equals(s.toLowerCase())){
companiesNewList.add(company);
}
}
adapter.updateList(companiesNewList);
return true;
}
Also, i have wrote a method in my RecyclerViewAdapter to update the list:
public void updateList(List<Company> list){
companies = new ArrayList<>();
companies.addAll(list);
notifyDataSetChanged();
}
What am i doing wrong?