Call us at +91-9810869242

Business Blog

What We have Done Lately

Social Media Login Integration in Android App- Part I (Facebook Login)

Share Us :

So what is the first thing we all do in the morning. Getting up, switching on the internet and opening your social media accounts. The very existence of social media and it’s ever growing usage has made it an amazing platform to be used as a basic information retriever for the developers. So, if you too are planning to integrate a social media login into your android app, here is a basic guide that will explain every minor details, with great simplicity.

So first step, Create a new Project or if you want to add it to your existing application skip this step(Keep in mind the minimum required  API level 15(Android 4.0.3 ).

Now go to your app’s  built.gradle(Module:app).

Add the following before ‘dependencies’ code to it :-

repositories { mavenCentral() }

and add the following dependencies in it  :-

compile ‘’

Now, compile the gradle to see for any errors. If no errors are present proceed further, otherwise check you sdk version and gradle dependencies.

Go to the application class from which you want to launch the Facebook login and add the following code in the ‘onCreate’ method:-

// Add this to the header of your file:
import com.facebook.FacebookSdk;                             //import this 

public class MyApplication extends Application {
    // Updated your class body:
    public void onCreate() {
        // Initialize the SDK before executing any other operations,
        FacebookSdk.sdkInitialize(getApplicationContext());  //add this line
        AppEventsLogger.activateApp(this);                   //add this line

Now go to and log in or make a facebook id and register your app and that will generate an app id save that in your strings.xml with a name ‘facebook_app_id’ . Now go to your AndroidManifest.xml file and add:-

<uses-permission android:name="android.permission.INTERNET"/>
<application android:label="@string/app_name" ...>           //where app_name is the name of your app that you have to create in your strings.xml
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>

Also add a FacebookActivity in your AndroidManifest.xml

<activity android:name="com.facebook.FacebookActivity"
          android:label="@string/app_name" />

Now you have to generate an android key hash for the facebook app which you can easily get by putting this code in any of your application methods and running the application once:-

try {
              info = getPackageManager().getPackageInfo("in.dezyne.mockinterview", PackageManager.GET_SIGNATURES);
              for (Signature signature : info.signatures) {
                  MessageDigest md;
                  md = MessageDigest.getInstance("SHA");
                  String something = new String(Base64.encode(md.digest(), 0));
                  //String something = new String(Base64.encodeBytes(md.digest()));
                  Log.e("hash key", something);
          } catch (PackageManager.NameNotFoundException e1) {
              Log.e("name not found", e1.toString());
          } catch (NoSuchAlgorithmException e) {
              Log.e("no such an algorithm", e.toString());
          } catch (Exception e) {
              Log.e("exception", e.toString());

Now place that hash key in your facebook app hash key and save changes. Now you are almost done place this custom facebook button the activity or fragments xml from where you want the user to log in.

    android:layout_marginBottom="30dp" />

Now in the ‘onCreateView’ of the activity or fragment, place the following code:-

public View onCreateView(
        LayoutInflater inflater,
        ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.splash, container, false);

    loginButton = (LoginButton) view.findViewById(;
    // If using in a fragment
    // Other app specific specialization

    // Callback registration
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        public void onSuccess(LoginResult loginResult) {
            // App code

        public void onCancel() {
            // App code

        public void onError(FacebookException exception) {
            // App code

You then need to call FacebookSdk.sdkInitialize to initialize the SDK, and then callCallbackManager.Factory.create to create a callback manager to handle login responses. Here’s an example of adding the callback in a fragment:

public class MainActivity extends FragmentActivity {
    CallbackManager callbackManager;
    public void onCreate(Bundle savedInstanceState) {
        callbackManager = CallbackManager.Factory.create();
        LoginButton loginButton = (LoginButton) view.findViewById(;
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { ... });

Now you add the callback to your activity or fragment’s onCreate() method:

public void onCreate(Bundle savedInstanceState) {

    callbackManager = CallbackManager.Factory.create();

            new FacebookCallback<LoginResult>() {
@Override public void onSuccess(LoginResult loginResult) { // App code } @Override public void onCancel() { // App code } @Override public void onError(FacebookException exception) { // App code } }); }

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

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

After all these steps you are good to go with the facebook login integration in your android app. But you can also customize the permission and the button appearance of the facbook login button. I hope this tutorial will help you with the facebook integration and have helped you. So this is it for this post, see you later with the next part of this blog with another social media platform login integration.

Share Us :
August 11, 2016
No Comments