Sharing on Android

This guide details how to enable sharing from your Android app to Facebook. When someone shares from your app, their content appears on their Timeline and may appear in their friends' News Feeds.

Image

People can also share content from your app to Facebook Messenger.

Prerequisites

Before you can share to Facebook from your app, you'll need to:

For details on these requirements, see Android - Getting Started.

You also need to set up a ContentProvider in your AndroidManifest.xml where {APP_ID} is your app ID:

<provider android:authorities="com.facebook.app.FacebookContentProvider{APP_ID}"
          android:name="com.facebook.FacebookContentProvider"
          android:exported="true"/>

When you implement sharing, your app should not pre-fill any content to be shared. This is inconsistent with Facebook Platform Policy, see Facebook Platform Policy, 2.3.

Modeling Content

Versions 4.0+ of the Facebook SDKs have new models for sharing content. Each type of content people want to share has a class you can use to represent it. After you model the content, add a sharing interface to your app.

When people share links from your app to Facebook, it includes attributes that show up in the post:

  • a contentURL, the link to be shared
  • a contentTitle that represents the title of the content in the link
  • a imageURL, the URL of thumbnail image that will appear on the post
  • a contentDescription of the content, usually 2-4 sentences
Image

Build your share content for links into the ShareLinkContent model. For a list of all attributes, see ShareLinkContent reference.

ShareLinkContent content = new ShareLinkContent.Builder()
        .setContentUrl(Uri.parse("https://developers.facebook.com"))
        .build();

Note: If your app share links to the iTunes or Google Play stores, we do not post any images or descriptions that you specify in the share. Instead we post some app information we scrape from the app store directly with the Webcrawler. This may not include images. To preview a link share to iTunes or Google Play, enter your URL into the URL Debugger.

Photos

People can share photos from your app to Facebook with the Share Dialog or with a custom interface.

  • The photos must be less than 12MB in size
  • People need the native Facebook for Android app installed, version 7.0 or higher
Image

Build your share content for photos into the SharePhotoContent model. For a list of all attributes, see SharePhotoContent reference.

Bitmap image = ...
SharePhoto photo = new SharePhoto.Builder()
        .setBitmap(image)
        .build();
SharePhotoContent content = new SharePhotoContent.Builder()
        .addPhoto(photo)
        .build();

Videos

People using your app can share videos to Facebook with the Share dialog or with your own custom interface.

Videos must be less than 12MB in size.

Build your share content for videos into the ShareVideoContent model. For a list of all attributes, see ShareVideoContent reference.

Uri videoFileUri = ...
ShareVideo = new ShareVideo.Builder()
        .setLocalUrl(videoUrl)
        .build();
ShareVideoContent content = new ShareVideoContent.Builder()
        .setVideo(video)
        .build();

Add Sharing Interfaces

After you handle content by building a model, you can either trigger a Facebook sharing interface or build your own that calls the Graph API.

Buttons

Facebook offers native buttons for Android for triggering sharing.


Like Button

The Like button is a quick way for people to share content with their friends. A tap on the Like button will 'like' pieces of content from your app and share them on Facebook. To add a Like button add the following code snippet to your view:

LikeView likeView = (LikeView) findViewById(R.id.like_view);
likeView.setObjectIdAndType(
    "https://www.facebook.com/FacebookDevelopers",
    LikeView.ObjectType.PAGE);

Share Button

The Share button will call a Share dialog. To add a Share button add the following code snippet to your view:

ShareButton shareButton = (ShareButton)findViewById(R.id.fb_share_button);
shareButton.setShareContent(content);

Send Button

The Send button lets people privately send photos, videos and links to their friends and contacts using the Facebook Messenger. The Send button will call a Message dialog. To add a Send button to your view add the following code snippet to your view:

SendButton sendButton = (SendButton)findViewById(R.id.fb_send_button);
sendButton.setShareContent(shareContent);
sendButton.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() { ... });

Share Dialog

The Share dialog switches to the native Facebook for Android app, then returns control to your app after a post is published. If the Facebook app is not installed it will automatically fallback to the web-based dialog.

ShareDialog.show(activityOrFragment, content);

For example, to show the ShareDialog for a link in your activity, create a ShareDialog instance in your onCreate method:

public class MainActivity extends FragmentActivity {
    CallbackManager callbackManager;
    ShareDialog shareDialog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        shareDialog = new ShareDialog(this);
        // this part is optional
        shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() { ... });
    }

Then show the ShareDialog:

if (ShareDialog.canShow(ShareLinkContent.class)) {
    ShareLinkContent linkContent = new ShareLinkContent.Builder()
            .setContentTitle("Hello Facebook")
            .setContentDescription(
                    "The 'Hello Facebook' sample  showcases simple Facebook integration")
            .setContentUrl(Uri.parse("http://developers.facebook.com/android"))
            .build();

    shareDialog.show(linkContent);
}

Finally call the SDK's callbackManager in your onActivityResult to handle the response:

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

Message Dialog

The Message dialog switches to the native Messenger for Android app, then returns control to your app after a post is published.

MessageDialog.show(activityOrFragment, content);

Custom Interface

If you want to use your own interface for sharing, you need to:

Here's the code for posting a link to Facebook from your own interface:

ShareApi.share(content, null);

Advanced Topics

Built-In Share Fallbacks

In past versions of the SDK for Android, your app had to check for a native, installed Facebook app before it could open the Share Dialog. If the person didn't have the app installed, you had to provide your own code to call a fallback dialog.

Now the SDK automatically checks for the native Facebook app. If it isn't installed, the SDK switches people to their default browser and opens the Feed Dialog. If someone wants to share an Open Graph story, the SDK opens the Web Share Dialog.

With App Links you link back to your app from Facebook posts published from your app. When people click a Facebook post published from your app, it opens your app, and you can even link to specific content within the app.

Sharing Open Graph Stories

Open Graph Stories let people share rich, structured content through a strongly typed API defined by Open Graph actions and objects. These posts link back to your app or to your app's App Store page.

To learn more about how to configure Open Graph stories, see Open Graph Stories in Android.

Build your share content for Open Graph actions into the ShareOpenGraphContent model. For a list of all attributes, see ShareOpenGraphContent reference.

ShareOpenGraphObject object = new ShareOpenGraphObject.Builder()
        .putString("og:type", "fitness.course")
        .putString("og:title", "Sample Course")
        .putString("og:description", "This is a sample course.")
        .putInt("fitness:duration:value", 100)
        .putString("fitness:duration:units", "s")
        .putInt("fitness:distance:value", 12)
        .putString("fitness:distance:units", "km")
        .putInt("fitness:speed:value", 5)
        .putString("fitness:speed:units", "m/s")
        .build();
ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
        .setActionType("fitness.runs")
        .putObject("fitness:course", object)
        .build();
ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
        .setPreviewPropertyName("fitness:course")
        .setAction(action)
        .build();