Android ShareActionProvider Tutorial

person Vinothkumar Arputharajfolder_openAndroidaccess_time March 16, 2015

In this tutorial we are going show how to use ShareActionProvider. Android Developer website gives the following definition:

This is a provider for a share action. It is responsible for creating views that enable data sharing and also to show a sub menu with sharing activities if the hosting item is placed on the overflow menu.

See flowing Steps :

  1. Create menu item main.xml under res/menu/ folder
  2. Add android:actionProviderClass=”android.widget.ShareActionProvider” attribute to the menu item
  3. In MainActivity.java override onCreateOptionsMenu method to add your code implementation
  4. Set the share action provider using setShareIntent

1. Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shareactionprovider"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.shareactionprovider.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

2. Activity layout file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="178dp"
        android:text="Demonstrate Share Action Provider . Touch overflow button in Actionbar == > menu_share "
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

3. Main Activity code

package com.example.shareactionprovider;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
public class MainActivity extends Activity {
	 private ShareActionProvider mShareActionProvider;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate menu resource file.
          getMenuInflater().inflate(R.menu.main, menu);
          // Locate MenuItem with ShareActionProvider
          MenuItem item = menu.findItem(R.id.menu_item_share);
          // Fetch and store ShareActionProvider
          mShareActionProvider = (ShareActionProvider) item.getActionProvider();
          setShareIntent(createShareIntent());
          // Return true to display menu
          return true;
     }
     // Call to update the share intent
     private void setShareIntent(Intent shareIntent) {
          if (mShareActionProvider != null) {
               mShareActionProvider.setShareIntent(shareIntent);
          }
     }
     private Intent createShareIntent() {
          Intent shareIntent = new Intent(Intent.ACTION_SEND);
          shareIntent.setType("text/plain");
          shareIntent.putExtra(Intent.EXTRA_TEXT,
                    "http://stackandroid.com");
          return shareIntent;
     }
}

4. Demonstration
Share action provider

Share action provider
Share action provider

Share action provider items

Share action provider 1
Share action provider 1

Comment

  1. Thanks, your example helped me a lot

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">