ActionBarSherlock requesting search

I`m trying to request search from ActionBarSherlock.

My class extends SherlockListActivity.

This is how I`m adding Menu button to the ActionBar:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("Save")
            .setIcon(R.drawable.ic_action_search)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
                MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        return true;
    }

The problem is that I am unable to invoke it by id:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId())  {
            case ?????: {
                onSearchRequested();
                return true;
            }
        }
        return true;
    }

How people usually solve this problem?

Thanks.

Answers


If you inflate you menu from an XML menu file you will set an id which you can then use in your onOptionsItemSelected method. Like this:

XML

<menu xmlns:android="http://schemas.android.com/apk/res/android">     
    <item android:id="@+id/menu_search"
    android:icon="@drawable/ic_menu_search"
    android:showAsAction="ifRoom"
    android:actionViewClass="android.widget.SearchView" android:title="@string/search"/>
</menu>

Fragment

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.menu_search:
            onSearchRequested();

            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

Need Your Help

When handling a flash.events.MouseEvent, how do I find the localX/Y with respect to the currentTarget?

flex flash actionscript

I have a component which draws a grid of things and I want a small highlight square to follow the mouse around highlighting the square that the mouse is currently over.

I'm doing something wrong -- Nested Types

swift

Alright so I'm practicing nested types, and I'm not sure where I'm going wrong here, it's probably something really simple, but it's just not coming to me right now, so I thought I'd ask for help. ...