Sharing on Android

After you integrate Facebook Login, Facebook Sharing, or Facebook Gaming, certain App Events are automatically logged and collected for Events Manager, unless you disable Automatic App Event Logging. We recommend all app developers using Facebook Login, Facebook Sharing, or Facebook Gaming to understand how this functionality works. For details about what information is collected and how to disable Automatic App Event Logging, see Automatic App Event Logging.

This guide explains how to enable users of your Android app to share from your app to Facebook. When someone shares from your app, the content that they share appears on their Timeline. Content that your users share to their Timeline can also appear in the Feeds of their friends. Users can also share content from your app to Facebook Messenger.

When you implement sharing, your app should not pre-fill any content to share. Pre-filling content is inconsistent with the Developer Policies.

The following example photos show the sharing dialog in your app on the left and the resulting post in the Facebook app on the right.

Getting Started with Sharing

The Sharing SDK for Android is a component of the Facebook SDK for Android.

To use the Facebook Sharing SDK in your project, make it a dependency in Maven.

  1. In your project, open your_app | Gradle Scripts | build.gradle (Project) and add the following repository to the buildscript { repositories {}} section:

    mavenCentral() 
    
  2. In your project, open your_app | Gradle Scripts | build.gradle (Module: app) and add the following compile statement to the dependencies{} section:

    compile 'com.facebook.android:facebook-share:latest.release'
    
  3. Build your project.

  4. Get your Facebook App ID properly configured and linked to your Android app.

  5. Generate an Android development key hash and add it to the Sample Apps page of your developer settings. For details, see Create a Development Key Hash and Running Sample Apps.

  6. Add a ContentProvider to your AndroidManifest.xml file and set {APP_ID} to your app ID:

    <provider android:authorities="com.facebook.app.FacebookContentProvider{APP_ID}"
    android:name="com.facebook.FacebookContentProvider"
    android:exported="true"/>
    
  7. If your application targets Android 11 or later, add the following queries block to your AndroidManifest.xml file to make the Facebook App visible to your App:

    <queries>
    <provider android:authorities="com.facebook.katana.provider.PlatformProvider" /> 
    </queries>
    
  8. Add a Facebook Activity to your project and include it in your AndroidManifest.xml file.

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 a contentURL with the link to be shared. Build your share content for links into the ShareLinkContent model. For a list of all attributes, see ShareLinkContent reference.

Here's an example of how you can trigger the share:

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

To preview a link share to Google Play or the App Store, enter your URL into the Sharing Debugger.

If your app share contains a link to any app on Google Play or the App Store, the description and image included in the share will be ignored. Instead, we will scrape the store directly for that app's title and image (and if there is no image, the share won't include one).

Photos

People can share photos from your app to Facebook with the Share Dialog. In order to share, they must have the native Facebook for Android app installed, version 7.0 or higher.

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.

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();

Multimedia

People can share a combination of photos and videos from your app to Facebook with the Share Dialog. Note the following:

  • People need the native Facebook for Android app installed, version 71 or higher.
  • People can share a maximum of 6 photos and videos at a time.

Build your multimedia share content with the ShareMediaContent model. For a list of all attributes, see ShareMediaContent reference.

SharePhoto sharePhoto1 = new SharePhoto.Builder()
    .setBitmap(...)
    .build();
SharePhoto sharePhoto2 = new SharePhoto.Builder()
    .setBitmap(...)
    .build();
ShareVideo shareVideo1 = new ShareVideo.Builder()
    .setLocalUrl(...)
    .build();
ShareVideo shareVideo2 = new ShareVideo.Builder()
    .setLocalUrl(...)
    .build();

ShareContent shareContent = new ShareMediaContent.Builder()
    .addMedium(sharePhoto1)
    .addMedium(sharePhoto2)
    .addMedium(shareVideo1)
    .addMedium(shareVideo2)
    .build();

ShareDialog shareDialog = new ShareDialog(...);
shareDialog.show(shareContent, Mode.AUTOMATIC);

Add Sharing Interfaces

After you handle content by building a model, trigger a Facebook sharing interface.

Buttons

Facebook offers native buttons for Android for triggering sharing.


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);

Share Dialog

The Share dialog switches to the native Facebook for Android app, then returns control to your app after a post is published. Depending on the SDK you're using, people may need tap the back arrow icon to return to your app. If the Facebook app is not installed, the Share dialog automatically falls back 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);
        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()
            .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);
}

If you are using AndroidX activities or fragments, you don't have to override onActivityResult.

Message Dialog

The Message dialog switches to the native Messenger for Android app, then returns control to your app after a post is published. Depending on the SDK you're using, people may need tap the back arrow icon to return to your app.

MessageDialog.show(activityOrFragment, content);

Hashtags

You can specify a single hashtag to appear with a shared photo, link, or video. This hashtag also appears in the Share dialog, and people have the the opportunity to remove it before publishing.

The following is an example of adding a hashtag to a link share.

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

Advanced Topics

Built-In Share Fallbacks

In past versions of the Facebook 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.

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.