I am trying to “Search” in ActionbarSherlock ver 4.2. ActionbarSherlock has backported SerchView in the latest version.
I have the following code in onCreateOptionsMenu of SherlockListFragment
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Place an action bar item for searching.
SearchView searchView = new SearchView(getSherlockActivity().getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search Friends");
searchView.setIconified(true);
menu.add(Menu.NONE, Menu.FIRST, Menu.FIRST, "Refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, Menu.FIRST + 1, Menu.FIRST + 1, "Search")
.setIcon(R.drawable.abs__ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
and the following code in the
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST:
Toast.makeText(getActivity(),"FIRST", Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST + 1:
Toast.makeText(getActivity(),"FIRST+1", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
I am getting Toast when I click on the “Refresh” button in the ABS but no response when when I click on the “search” it expands and gets converted into EditText but Toast is not fired.
My Question
How to integrate “search” in Actionbar with ABS?
It works.
For implementation of SearchView we need to implement callback interface like this
in this particular case we need to pass the text entered by the user to the CursorLoader and let it reload the cursor with the appropriate results.