2

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?

csirkeautomata
  • 177
  • 3
  • 16

1 Answers1

0
     @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.search_menu, menu);
    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) menu.findItem(R.id.action_search)
            .getActionView();
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getActivity().getComponentName()));
    searchView.setMaxWidth(Integer.MAX_VALUE);
    // listening to search query text change
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            // filter recycler view when query submitted
            getFilter().filter(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            // filter recycler view when text is changed
            getFilter().filter(query);
            return false;
        }
    });

    super.onCreateOptionsMenu(menu, inflater);
}
Bolu
  • 216
  • 3
  • 15
  • it is instead of "updateList" method? (goes to the adapter?) I'm pretty sure not the updating the problem.. i have problems with implementing the searchView. As I mentioned i have two solutions.. one is throwing a NullPointer.. and with the other solution..i am not able to see my SearchView int the toolbar. – csirkeautomata Jul 23 '19 at 09:36
  • check the updated answer try using menu instead of menuItem – Bolu Jul 23 '19 at 11:50
  • in this version i do not need my fragment to implemets SerachView, am i right? :) – csirkeautomata Jul 23 '19 at 13:14
  • yup you don't need to – Bolu Jul 23 '19 at 13:22
  • i have tried your solution, but i am still not able to see my SearchView in the toolbar. It's looks like onCreateOptionMenu is not loaded when my fragment constructed. I am pretty sure this answer would be the best solution.. but can u check my question again? I don't know why onCreateOptionsMenu is not inflating the searchSview into my toolbar. – csirkeautomata Jul 24 '19 at 15:21