2015-12-26

Objective

In this tutorial, we will learn step-wise process of sharing the content from your android app to Facebook. To learn about how to Android Facebook Integration and Login.

Step 1

Introduction

Facebook, nowadays, became an integral part of everyone’s lifestyle. Using this platform, users can connect and share with their friends and the world whenever they want. To benefit app developers with these features, Facebook created various APIs which developers can use to connect their app with Facebook. So, we can integrate Facebook to our android app and can use its various features such as login, like, share, comment etc.

Step 2

Creating a new project

Before you can share to Facebook from your app, we’ll need to create a new project in android studio and integrate Facebook to it using Android Facebook Integration and Login Tutorial.

Step 3

Set up ContentProvider

While implement share feature, we also need to set up a ContentProvider in your AndroidManifest.xml. Without using ContentProvider, your app cannot share any content on your Facebook timeline.

We will select an image from mobile gallery to share on Facebook timeline so we need to add app permission to select image from mobile gallery.

Use the following code in your AndroidManifest.xml. And replace 1669185xxxxxx with your App_id.

Step 4

Add Android App permission

We will set a permission to share any content from your app. We need to add email, publish_actions permissions, before we made a login process in MainActivity.java file. By using this code we can set permissions:

Step 5

Making Layout

Now, we need to create a layout with two button. By using one button user can share content and by using another button he can share image on Facebook timeline.

Add the following code in your app_bar_home.xml file:

Your final app_bar_home.xml file:



Step 6

Initalize the Facebook Sdk

Now we need to initialize the facebook SDK by calling FacebookSdk.sdkInitialize and then callback manager will handle the login responses by calling CallbackManager.Factory.create Add the following line of code to the HomeActivity.java file and call it before writing any line of code into the onCreate method.

Step 7

Enable Logging app activations

The Facebook SDK provides a helper method to log app activation event. Using these logged events, you can observe how frequently your app is being activated by the users, how much time they spend using your app.
Now open your HomeActivity.java file and add the Facebook SDK’s app activation helper to the onResume method.

Step 7.1

App activation helper

Step 7.2

App deactivation helper

Add the app deactivation helper in the onSuspend method of each activity that calls  activateApp in its onResume method :

Step 8

Sharing LinkContent on Facebook

To share the content on the Facebook timeline from your app, we will initialize a share dialog box which is provided by Facebook SDK.

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. To show the ShareDialog for a link in your activity, create a ShareDialog instance in your onCreate method then show the ShareDialog. Use the following code in HomeActivity.java



Step 8.1

Handle the response

Call the SDK’s callbackManager in HomeActivity.java onActivityResult method to handle the response for sharing content:

Step 9

Sharing Photo on Facebook

Step 9.1

Prerequisites

We can share photos from your app to Facebook with the Share Dialog.

The photos must be less than 12MB in size.

We need the native Facebook for Android app installed, version 7.0 or higher.

Build your share content for photos into the SharePhotoContent model.

Step 9.2

Select image from gallery

To share photos from your app to Facebook with the Share Dialog, we will select an image from mobile gallery to share on Facebook. We need to create a dialog box with multiple options to choose an image from mobile gallery or  capture image.

For the selection of image we are using android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI Intent and for capture of image we are using MediaStore.ACTION_IMAGE_CAPTURE Intent.

Use the following code to create a dialog box with multiple options

Step 9.3

Convert image in Bitmap format

After completion of the selection or capture of the image, we will get the response data through onActivityResult method then onActivityResult method send the response data to onSelectFromGalleryResult and onCaptureImageResult method to convert image into bitmap format.

By Using following method, we will select an image from gallery and capture an image:

Step 9.4

Adding content into SharePhotoContent model

We will share the content for photo by using SharePhotoContent model which is provided by Facebook SDK. SharePhotoContent model support bitmap photo. So We have already converted image into bitmap format.

Step 10

Final Activity Code

14. Here is the final code of HomeActivity.java

Show more