トラッキング コード

1/19/2012

Adding share action in ActionBar

See Gallery app



I introduce that how to add share action in ActionBar such as Galley app.


Creat menu resouce

You must to creat menu resouce with "android:actionProviderClass="android.widget.ShareActionProvider".

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/menu_share"
        android:title="share"
        android:icon="@android:drawable/ic_menu_share"
        android:showAsAction="ifRoom"
        android:actionProviderClass="android.widget.ShareActionProvider">
    </item>
</menu>

Adding Intent action

You must to add intent action with ShareActionProvider.class in onCreateOptionsMenu method.

public boolean onCreateOptionsMenu(Menu menu){
     getMenuInflater().inflate(R.menu.menu, menu);
        mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_share).getActionProvider();

        // If you use more than one ShareActionProvider, each for a different action,
        // use the following line to specify a unique history file for each one.
        // mShareActionProvider.setShareHistoryFileName("custom_share_history.xml");

        // Set the share intent
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "shareIntent Test");
        
        mShareActionProvider.setShareIntent(shareIntent);

        return true;
    }


No comments:

Post a Comment