Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Adding a Share Action to your ActionBar

Author: Ian Darwin
Published? false -- FormatLanguage: W

Problem:

You want to add the standard Share icon to your ActionBar and have it handle an application-provided Intent.

Solution:

Use the actionProviderClass attribute in a menu item, set up an Intent for it to process, and pass the Intent into the ActionProvider. It really is that simple!

Discussion:

Sharing information is one of the canonical uses of mobile and computing devices. Having one application use another to handle data is a prime feature of the Android platform. Android offers a "Share" menu that lets you pass text, images, or almost anything else, off to any of a number of applications to be handled.

Our example exports a short String for sharing as "plain text"; this will be sharable by quite a few applications, but Android will choose the "most popular" to put at the top of the sharing menu.

We start by adding a menu item which will go into the ActionBar.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+id/menu_item_share"
        android:showAsAction="ifRoom"
        android:title="@string/action_share"
        android:actionProviderClass="android.widget.ShareActionProvider" />
...
</menu>

In our onCreate() method, we create an Intent with the action of ACTION_SEND, a type of plain text, and an Extra of the string we want to share. There is nothing special about text - this mechanism can share almost any kind of data, as long as there's at least one app has registered with an Intent Filter for the given content type.

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mShareIntent = new Intent();
		mShareIntent.setAction(Intent.ACTION_SEND);
		mShareIntent.setType("text/plain");
		mShareIntent.putExtra(Intent.EXTRA_TEXT, "From me to you, this text is new.");
	}

Finally, in our menu creation method, we find the MenuItem by its ID, ask it for its ActionProvider (this is where API level 14 is required!); if we find that, we just add the share intent to it!

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
		
	    // Find the MenuItem that we know has the ShareActionProvider
	    MenuItem item = menu.findItem(R.id.menu_item_share);

	    // Get its ShareActionProvider
	    mShareActionProvider = (ShareActionProvider) item.getActionProvider();

	    // Connect the dots: give the ShareActionProvider its Share Intent
	    if (mShareActionProvider != null) {
	        mShareActionProvider.setShareIntent(mShareIntent);
	    }
	        
	    // Return true so Android will know we want to display the menu
		return true;
	}

That really is all there is to it.

When we first run the app it looks like this:

Tap the Share icon and the Share Menu appears - all courtesy of the ShareActionProvider! As mentioned, the most likely apps are at the top of the list; the rest are delegated to the "See All" section.

I picked the Messaging app and, just as a quick reality check, sent it to myself.

The message arrives!

Note that if you go back to the app that started the sharing, if there is room in the ActionBar, the app you chose to share with (in my case, Messaging) appears beside the Sharing icon - a neat optimization!

Download:

The source code for this project is in the Android Cookbook repository at http://github.com/IanDarwin/Android-Cookbook-Examples, in the subdirectory ShareActionProviderDemo.
No records found.