Facebook integration in Android

In this tutorial, we will learn how to Integrate Facebook in an Android Application.

Facebook Login provides a convenient and secure way for people to log in to an app without having to go through a sign-up process first.


All apps that use the Facebook SDK must be registered with Facebook. Log in to the Facebook Developers website.


Select My App drop down and click Add a New App in the top right and select Android as a platform in the Add a New App popup.


Type an App name in the Quick start for Android screen and Select Create New Facebook App ID.


In the Guide line, skip Import SDk, and Add SDK sections, you can read if you want.

Scroll Down to the App Info section, and Type Project Package name and Default Activity Class Name.


If your App is not Live, then an alert with Google Play Package Name verification failed shows up. You can ignore this message by selecting Use this Package Name option.

In the Key hash section, we need to supply Android key hash of our development environment. If your app has already been published, you should add your release key hash too.


To fill in the Key Hashes field, open a terminal window and run the keytool command to generate a key hash using the debug keystore located at ~/.android/debug.keystore. This is what the command should look like.

The default password for the debug keystore is android. Enter that password when prompted, or by coding:

The output of the command should be a string of 28 characters.

You can skip the App Event section, as i said, you can read if you want.

Next step is to Finish the Guide Line. Select any of their API docs and go through it or you continue this tutorial.


Before we going to the next stage, on your App Dashboard, make a note of Application ID, because you will be needing it later in this tutorial.


The Facebook SDK is available on Maven Central. To use this repository, edit the build.gradle file in your project’s app directory and add the following code to it before the list of dependencies:

You can now add the Facebook SDK to your project as a compile dependency. Add the following code to the list of dependencies:


Using Facebook API’s

Add Facebook Login:

There are two ways to login,

  • LoginButton class – Which provides a button you can add to your UI. It follows the current access token and can log people in and out.
  • LoginManager class – To initiate login without using a UI element.

The simplest way to add Facebook Login to your app is to add LoginButton from the SDK. This is a custom view implementation of a Button. You can use this button in your app to implement Facebook Login.

The SDK needs to be initialized before using any of its methods. You can do so by calling sdkInitialize and passing the application’s context to it. Add the following code to the onCreate method of your Activity:

Next, initialize your instance of CallbackManager using the CallbackManager.Factory.create method. CallbackManager use to route calls back to the Facebook SDK and your registered callbacks.


Add the button to your layout XML file with the full class name, com.facebook.widget.LoginButton:

Initialize the widget as usual using findViewById:


It’s time to create a callback to handle the results of the login attempts and register it with the CallbackManager. Custom callbacks should implement FacebookCallback. The interface has methods to handle each possible outcome of a login attempt:

  • If the login attempt is successful, onSuccess is called.
  • If the user cancels the login attempt, onCancel is called.
  • If an error occurs, onError is called.

If login succeeds, the LoginResult parameter has the new AccessToken, and the most recently granted or declined permissions.

Then in onActivityResult() forward the login results to the callbackManager created in onCreate():

The application ID you received when you registered your app should be added as a string in your project’sres/values/strings.xml. For this tutorial, call the string facebook_app_id.

Add the application ID as meta-data.

Define FacebookActivity as another Activity that belongs to your app. It handles most of the configuration changes itself. You need to mention that using the configChanges attribute.


Access Tokens and Profiles

Your app can only have one person at a time logged in and LoginManager sets the current AccessTokenand Profile for that person. The FacebookSDK saves this data in shared preferences and sets it during SDK initialization. You can see if a person is already logged in by checkingAccessToken.getCurrentAccessToken() and Profile.getCurrentProfile().

Get Current Token

You can load AccessToken.getCurrentAccessToken with the SDK from cache or from an app.

Track Access Tokens

If you want your app to keep up with the current access token and profile, you can implement AccessTokenTracker and ProfileTracker classes.

These classes call your code when access token or profile changes happen. Internally they use receivers, so you need to call stopTracking() on an activity or call a fragment’s onDestroy() method.


Get User Profile Details

To retrieve basic profile information, we need request permission using setReadPermission.

To get current user profile, use the Profile class from SDK which returns basic profile information about the user.


Profile Picture

There are three ways to retrieve a profile picture.
1. Using com.facebook.login.widget.ProfilePictureView in the XML

and set Profile Id from the profile class.

2. Using profile picture Uri from Profile class, getProfilePictureUri(int width, int height)

3. Retrieve from the Graph API where you need to pass, unique profile Id.

You can use types,

  1. large
  2. normal
  3. small
  4. square

From the Uri, you can download the profile picture.

 Add a Like Button

Like Button can be added from the LikeView class from the SDK, this class provides the UI for displaying the Facebook Like button and its associated components.

 Add the com.facebook.share.widget.LikeView class in the XML

To ‘LIKE’ a thing, set LikeView’s url through setObjectIdAndType method.

You can also set different styles by setLikeViewStyle(Style likeViewStyle) where likeViewStyle, should be either LikeView.STANDARD, LikeView.BUTTON or LikeView.BOX_COUNT.


Share Content

Facebook SDKs 4.0 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.

  • Photos class – SharePhotoContent model

  • Videos class – ShareVideoContent model

Build your share content for links, 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

Add the com.facebook.share.widget.ShareButton class in the XML

Initialize the widget and add the share content to it.

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.

finally, on clicklistener of the view,

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



fb_home fb_loginbutton device-2016-04-10-124543 device-2016-04-10-124629 device-2016-04-10-124820 device-2016-04-10-124726


Complete Source code available here.


About Durga Chiranjeevi

I’m a normal guy, engineer by education who is passionate about Programming and Internet. An android enthusiast, now moving for cross platforms and game development.