Butterknife bind SearchView from Menu
Asked Answered
B

1

12

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?

Boondocks answered 21/9, 2015 at 21:30 Comment(1)
A feature was requested to make this happen, but it was rejected by Jake Wharton. Please see issue #41 and #416 and said I want to keep this focused on assisting with view-realted things only.Hamforrd
D
9

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);
}
Dormouse answered 10/8, 2016 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.