Facebook Login for Android - Quickstart

The Facebook SDK for Android enables people to sign into your app with Facebook Login. When people log into your app with Facebook they can grant permissions to your app so you can retrieve information or perform actions on Facebook on their behalf.

When you develop your app with version 4.14.0 of the Facebook SDK for Android, Facebook Login has automatic integration with Facebook Lite. If people don't have the Facebook Android app installed, Facebook Login uses Facebook Lite instead to display the login screen and get credentials. Previous SDKs required that people have the Facebook app installed.

Follow the steps below to add Facebook Login to your app.

1. Login

Please log in to Facebook to create apps or register as a developer.

2. Download the Facebook App

Download the Facebook app by clicking the button below.

3. Import the Facebook SDK

When you use the Facebook SDK, events in your app are automatically logged and collected for Facebook Analytics unless you disable automatic event logging. For details about what information is collected and how to disable automatic event logging, see Automatic App Event Logging.

To use the Facebook SDK in a project, add it as a build dependency and import the Facebook SDK packages.

Important: If you're adding the SDK to an existing project, start at step 3.
  1. Go to Android Studio | New Project | Minimum SDK.
  2. Select API 15: Android 4.0.3 (IceCreamSandwich) or higher and create your new project.
  3. In your project, open your_app | Gradle Scripts | build.gradle (Project) and add the following to the buildscript { repositories {}} section to download the SDK from the Maven Central Repository:
    mavenCentral() 
  4. In your project, open your_app | Gradle Scripts | build.gradle (Module: app) and add the following to the dependencies{} section to compile the latest version of the SDK:
    compile 'com.facebook.android:facebook-android-sdk:[4,5)'

    When you use the Facebook SDK, events in your app are automatically logged and collected for Facebook Analytics unless you disable automatic event logging. For details about what information is collected and how to disable automatic event logging, see Automatic App Event Logging.

  5. Build your project.
  6. Add the following statements to import the Facebook SDK packages:
    import com.facebook.FacebookSdk;
    import com.facebook.appevents.AppEventsLogger;
    

4. Edit Your Manifest

Create strings for your Facebook app ID and to enable Chrome Custom Tabs, and add them along with the FacebookActivity to your Android manifest.
  1. Open your /app/src/main/res/values/strings.xml file.
  2. Add the following:
    <string name="facebook_app_id">[APP_ID]</string>
    <string name="fb_login_protocol_scheme">fb[APP_ID]</string>
    
  3. Open the /app/manifest/AndroidManifest.xml file.
  4. Add a uses-permission element to the manifest:
    <uses-permission android:name="android.permission.INTERNET"/>
    
    
  5. Add the following meta-data element, an activity for Facebook, and an activity and intent filter for Chrome Custom Tabs after the application element. Replace @string/appname with the name of your Facebook App:
    <meta-data android:name="com.facebook.sdk.ApplicationId" 
            android:value="@string/facebook_app_id"/>
        
        <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
                    "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name" />
        <activity
            android:name="com.facebook.CustomTabActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="@string/fb_login_protocol_scheme" />
            </intent-filter>
        </activity>
    

5. Associate Your Package Name and Default Class with Your App

You need to login to complete this step.

6. Provide the Development and Release Key Hashes for Your App

You need to login to complete this step.

7. Enable Single Sign On for Your App

You need to login to complete this step.

8. Add the Facebook Login Button

The simplest way to add Facebook Login to your app is to add LoginButton from the SDK. The LoginButton is a UI element that wraps functionality available in the LoginManager. When someone clicks on the button, the login is initiated with the permissions set in the LoginManager. The button follows the login state, and displays the correct text based on someone's authentication state.
To add the Facebook Login button, first add it to your layout XML file with the full class name, com.facebook.widget.LoginButton:
<com.facebook.login.widget.LoginButton
    android:id="@+id/login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    android:layout_marginBottom="30dp" /> 
Then set up the button in your UI by adding it to a fragment and update your activity to use your fragment.
You can customize the properties of Login button and register a callback in your onCreateView() method. Properties you can customize includes LoginBehavior, DefaultAudience, ToolTipPopup.Style and permissions on the LoginButton. For example:
@Override
public View onCreateView(
        LayoutInflater inflater,
        ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.splash, container, false);

    loginButton = (LoginButton) view.findViewById(R.id.login_button);
    loginButton.setReadPermissions("email");
    // If using in a fragment
    loginButton.setFragment(this);    
    // Other app specific specialization

    // Callback registration
    loginButton.registerCallback(callbackManager, 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
        }
    });    
}
If you use the LoginButton in a fragment, you need to set the fragment on the button as shown by calling setFragment. Then call CallbackManager.Factory.create to create a callback manager to handle login responses.
public class MainActivity extends FragmentActivity {
    CallbackManager callbackManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        callbackManager = CallbackManager.Factory.create();
        LoginButton loginButton = (LoginButton) view.findViewById(R.id.usersettings_fragment_login_button);
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { ... });
    }
}
Finally, call callbackManager.onActivityResult to pass the login results to the LoginManager via callbackManager.

9. Register a Callback

To respond to a login result, you need to register a callback with either LoginManager or LoginButton. If you register the callback with LoginButton, don't need to register the callback on Login manager.
You add the callback to your activity or fragment's onCreate() method:
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    callbackManager = CallbackManager.Factory.create();

    LoginManager.getInstance().registerCallback(callbackManager,
            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   
                }
    });
}
If login succeeds, the LoginResult parameter has the new AccessToken, and the most recently granted or declined permissions.
You don't need a registerCallback for login to succeed, you can choose to follow current access token changes with the AccessTokenTracker class described below.
Then in onActivityResult() forward the login results to the callbackManager created in onCreate():
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}
Every activity and fragment that you integrate with the FacebookSDK Login or Share should forward onActivityResult to the callbackManager.

10. Check Login Status

Your app can only have one person at a time logged in, and LoginManager sets the current AccessToken and Profile for that person. The FacebookSDK saves this data in shared preferences and sets at the beginning of the session. You can see if a person is already logged in by checking AccessToken.getCurrentAccessToken() and Profile.getCurrentProfile().
You can load AccessToken.getCurrentAccessToken with the SDK from cache or from an app book mark when your app cold launches. You should check its validity at your Activity's onCreate method:
public class MainActivity extends FragmentActivity {
    CallbackManager callbackManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        callbackManager = CallbackManager.Factory.create();
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {...});
    }
}
Then you can later perform the actual login, such as in a custom button's OnClickListener:
      
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));
Finally, in the onActivityResult in your Activity, pass the result to the CallbackManager:
@Override
 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

Next Steps

Congrats, you've added Facebook Login to your Android app! Be sure to check out our other documentation pages for more advanced guides.
Track the access token and profile of your users.
Manage what data your app has access to through Facebook Login.
Having problems integrating Facebook Login? Check out a list of common problems and how to resolve them.
Submit your app for review to ensure the best possible Facebook experience for your app's audience.