Implementing SearchView in Toolbar with Fragments
Put this in your fragment
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
And you can get the SearchView like follows
@Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem mSearchMenuItem = menu.findItem(R.id.mi_search);
SearchView searchView = (SearchView) mSearchMenuItem.getActionView();
}
Related videos on Youtube
Vivek Mishra
Collaborative Mobile Developer with a reputation for building lasting relationships among team members, partnering across departments to produce highly effective content that meets desired outcomes. App-oriented architect with meticulous attention to detail in strategic design and problem-solving.
Updated on September 15, 2022Comments
-
Vivek Mishra over 1 year
CURRENT SCENARIO
My app home page consists of navigation drawer, therefore I am having views loaded as fragments. I also have search icon in toolbar. I implemented it in menu.xml. Next step I implemented
SearchView
for search icon by following answer on this question Implementing search in Toolbar.This is working fine as search view shows and can also be dismissed.
PROBLEM
I can implement search query for the search view but I cannot understand how to proceed. Problem is that
onCreateOptionsMenu
is inActivity
and all code for search view is in theActivity
. What I don't understand is data that has to be searched is inFragment
that is loaded inActivity
class. I will hit another webservice to get the search result but how would I inflate searched data inFragment
again. I can't understand how to proceed in this situation. -
Vivek Mishra over 8 yearsHo will I get text written in searchview?? Should I have to implement textChangeListener
-
Malek Hijazi over 8 yearsImplement a query text listener on the searchview as follows: searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener())
-
Vivek Mishra over 8 yearsI am getting superNotCalled error when navigate to the fragment
-
Malek Hijazi over 8 yearsreturn super.onPrepareOptionsMenu(menu); at the end of the onPrepareOptionsMenu
-
Vivek Mishra over 8 yearsIt's a void method how it can return?
-
Vivek Mishra over 8 yearsNow it says incompatible type on return statement. Required boolean found void
-
Vivek Mishra over 8 yearsLet us continue this discussion in chat.
-
SAndroidD about 7 yearsHi Malek I cant access the searchitem click event in fragment please help me
-
Dinith Rukshan Kumara almost 6 yearsThis work perfect on Navigation Drawer with Fragments. Thanks a lot!.