Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Butterknife bind SearchView from Menu

In my current Android project I use the Butterknife library to bind views and use onClick annotations for them. This all works great even in fragments but now I'm at the point where I can't find a solution:

I use the new ToolBar as ActionBar and inflate a Menu with a SearchView in it. For this SearchView I want to use the @OnTextChanged annotation but when I call the bind method with the ActionView of the menu item Butterknife tries to reinstanciate all Views again and of course in the ActionView it can not find any other Views of the RootLayout.

So is there a way to add only one View with Butterknife or can I get a View which contains all Views from my RootLayout and the ToolBarView so I can pass this View to the bind method? For example in Activites I can call findViewById also for menu IDs but if I use getView() from my Fragment it does not work. Any ideas for this?

like image 721
Cilenco Avatar asked Sep 21 '15 21:09

Cilenco


1 Answers

I think this is not possible since the SearchView is a menu item. The id you are using in the menu declaration identifies this view within the menu, not the activity's view, thats probably why Butterknife is no able to bind it.

I'm afraid you will have to do:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.bookings_list_menu, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

    searchView.setOnSearchClickListener(...);
    searchView.setOnCloseListener(...);
    searchView.setOnQueryTextListener(...);

    super.onCreateOptionsMenu(menu, inflater);
}
like image 93
josemigallas Avatar answered Oct 31 '22 11:10

josemigallas