2

I successfully implemented search view with action bar sherlock but just for curiosity, how can I do that using Android Annotations?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    SearchView searchView = new
            SearchView(getSupportActionBar().getThemedContext());
    searchView.setQueryHint(getString(R.string.hint_search_bar));
    menu.add("Search")
            .setIcon(R.drawable.icon_search)
            .setActionView(searchView)
            .setShowAsAction(
                    MenuItem.SHOW_AS_ACTION_IF_ROOM
                            | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    return true;
}

I created a xml menu and annotate my Activity with

@OptionsMenu(R.menu.menu_search)

<item
    android:id="@+id/menuSearch"
    android:actionViewClass="SearchView"
    android:icon="@drawable/icon_search"
    android:showAsAction="ifRoom|collapseActionView"
    android:title="@string/lbl_search">
</item>

but the search icon click doesn't open the search text field. I think the problem is this line

SearchView searchView = new
            SearchView(getSupportActionBar().getThemedContext());

that cannot be reproduced in xml. Am I right?

Raphael Oliveira
  • 7,751
  • 5
  • 47
  • 55

1 Answers1

4

When you do like below, I think it should work.

<item
android:id="@+id/menuSearch"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:showAsAction="ifRoom|collapseActionView"
android:title="@string/lbl_search">
</item>
tasomaniac
  • 10,234
  • 6
  • 52
  • 84