Using ShareActionProvider in Action Bar

July 21, 2012
By

In this article we will create a share button in the action bar of the MainActivity screen. For this we will use ShareActionProvider available in Android. In order to create the built in share action, we have to do two things. First, we need to create a menu item with property actionProviderClass having the value android.widget.ShareActionProvider. Second thing is to set a share intent for this menu . This can be done in the callback function onCreateOptions.

This application is developed in Eclipse 3.7.2 and is tested in Android Virtual Box ( Android x86 ) .

In the article titled “Adding ShareActionProvider to action bar in pre Honeycomb versions using Sherlock library“, we can see how to add share action to action bar in pre Honeycomb versions.


1. Create a new Android project namely ActionBarShareActionProvider

Create new Android Application

Figure 1 : Create new Android Application


2. Select Android build target

Select Android Build Target

Figure 2 : Select Android Build Target


3. Enter application details

Enter Application Details

Figure 3 : Enter Application Details


4. res/values/strings.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">ActionBarShareActionProvider</string>

    <string name="share">Share</string>
</resources>


5. res/menu/items.xml


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


6. src/in/wptrafficanalyzer/actionbarshareaction/mainActivity.java


package in.wptrafficanalyzer.actionbarshareactionprovider;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ShareActionProvider;

public class MainActivity extends Activity {

    private ShareActionProvider mShareActionProvider;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        /** Inflating the current activity's menu with res/menu/items.xml */
        getMenuInflater().inflate(R.menu.items, menu);

        /** Getting the actionprovider associated with the menu item whose id is share */
        mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();

        /** Setting a share intent */
        mShareActionProvider.setShareIntent(getDefaultShareIntent());

        return super.onCreateOptionsMenu(menu);

    }

    /** Returns a share intent */
    private Intent getDefaultShareIntent(){
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
        intent.putExtra(Intent.EXTRA_TEXT,"Extra Text");
        return intent;
    }
}


7. Application in Execution

Share Action Provider

Figure 4 : Share Action Provider



8. Download


9. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , , ,

11 Responses to Using ShareActionProvider in Action Bar

  1. tushar on October 20, 2012 at 3:15 pm

    thanks , i need it !

    • jasmin kevadiya on October 11, 2013 at 11:24 am

      thanks

  2. Harish on February 19, 2013 at 4:16 pm

    hi da
    post me how to change edittext background colors only border as green when clicked focused normal………..

  3. huy on April 10, 2014 at 11:45 am

    I want to share the screen. then how to

  4. hari on August 19, 2014 at 4:51 pm

    hai try uninstalling all the sharing application except your bluetooth and try your code again it wont work.. since you have installed many sharing application share action provider automatically invokes installed sharing application in the action bar. try with bluethooth alone there comes the real problem.

  5. hitesh on September 15, 2014 at 5:10 pm

    sir can please help me how to share image in watsup and facbook and twitter please give me idea..thank you

  6. Martin on January 9, 2015 at 2:45 am

    You set the text to share (EXTRA_TEXT) when onCreateOptionsMenu is called. What if you want to share dynamic content which is only known when the share button is actually clicked?

  7. gabbitto on March 28, 2015 at 5:35 am

    tanx… jst implemented mine.

  8. andrew on October 22, 2015 at 8:27 am

    how to change the default icon to become black color or other color? is that possible?

  9. samdoj on November 6, 2015 at 3:02 am

    I have been trying to get a share button using the AppCompat classes. I have code very similar to this, but my app uses a toolbar as an action bar. It crashes when I inflate the menu only if showAsAction !=”never”.
    If it does not show as an action, I get functionality, but not an icon.

Leave a Reply

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

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner