1

I'm trying to use one searchview from actitivity toolbar menu, to filter three fragments attached to it (It's a tabbed activity) at the same time and categorizing the results in the different fragments . Kind of like the way Instagram does theirs. I've tried inflating the onCreateOptionsMenu in each fragment, but this just starts a new instance of the search i.e (search icon is .istIconified(); I want the differnt tabs to show the query text of what ever was typed in it and perform the search at the same time.Can't seem to find this solution on SO, Any help or resource will be very much appreciated

XY-JOE
  • 1,574
  • 1
  • 11
  • 9
  • It would be nice if you provided a proper solution to your question. I am facing the same situation and I don't know how to go about it as the query string returns null. – sanjeev Dec 11 '18 at 06:01

1 Answers1

2

Well it is possible.

first of all use "setOnQueryTextListener"

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    MenuItem item = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);

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

        @Override
        public boolean onQueryTextChange(String newText) {
            Log.i("well", " this worked");
            return false;
        }
    });
}

After that pass this string to currently selected fragment by calling fragment method from the activity. click here to check how to call fragment method from activity

From fragment method you can do anything which you want to do for.

Sameer Donga
  • 988
  • 9
  • 24
  • thanks for the answer, it didn't solve the problem directly, but it gave me a clearer insight on how to go about it. My problem was being able to call the fragments from the parent activity since i was making use of FragmentPagerAdapter. But the link you provided gave me a clue on how to do that (not that it was done there). After successfully being able to call the fragments, now i can filter each fragment from parent activity – XY-JOE Aug 25 '18 at 12:19