0

I have added Share option to my ActionBarSherlock, this way:

public boolean onCreateOptionsMenu(final Menu menu) {

menu.add("Share")
    .setIcon(R.drawable.ic_title_share_default)
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}

and on clicking this Icon i want to do something. how can i track the click on this ShareIcon??

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226

1 Answers1

2

You should create an XML file to define menu items. For example mymenu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/share"
          android:icon="@drawable/ic_title_share_default"
          android:title="@string/share"
          android:showAsAction="ifRoom"/>
</menu>

Then in the onCreateOptionsMenu you will do:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.mymenu, menu);
    return true;
}

To handle the item selection:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.share:
            //do something for share
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

You can see more information in here:

http://developer.android.com/guide/topics/ui/menus.html

http://actionbarsherlock.com/usage.html

Community
  • 1
  • 1
Filipe Batista
  • 1,862
  • 2
  • 24
  • 39