ZOFTINO.COM android and web dev tutorials

Facebook Login Implementation for Android Apps

Facebook provides SDK for android integration using that you can easily provide facebook login option in your android apps so that users can use their facebook account and login to your app. They don’t need to create one more account for your app with facebook login option.

To implement facebook login option in your android app, you need to create facebook developers account and follow below steps to complete facebook integration in your app.

Create facebook app

After you login to your facebook developers account, click "my apps" drop down located at top right corner and then click "add a new app", fill the data and create app.

create facebook app

Facebook App Id

Go to "my apps" drop down located at top right corner and then click the app you created, you will see app dashboard and app id. You need facebook app id to configure facebook login in your android app.

facebook app id

Generate Key Hashes

You need to generate key hash which is sha1 hash of digital certificate that you use to sign your app. For development, android sdk creates debug keystore by default. You can generate key hash for debug digital certificate using below command. It prompts for password and default debug keystore password is android.

keytool -exportcert -alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore | openssl sha1 -binary | openssl base64

To generate key hash for release certificate, you need to use below command replacing alias and keystore paths with your release keystore info.

keytool -exportcert -alias -keystore | openssl sha1 -binary | openssl base64

Facebook App Settings

Select your facebook app, click setting in left navigation and fill details in android section like your android app package name, main activity and key hash generated by following above step.

facebook app settings android

Add Test Users

If you want to test your app when it is in development, you can add test user to your app in facebook and use the added test users to login from your android app.

facebook app add test user

Add Facebook SDK and Dependencies

Now you need to add facebook SDK to your project. You can do so by adding maven repository entry in your gradle build file.

repositories {

Then add below dependency also.

compile 'com.facebook.android:facebook-android-sdk:4.20.0'

Add Facebook App Id

You need to add facebook app id that you created in facebook developer’s account to your android project by adding meta-data entry to android manifest file like below. You can keep facebook app id in string.xml and refer to it in android manifest.xml file.

<meta-data android:name="com.facebook.sdk.ApplicationId"   android:value="@string/facebook_app_id"></meta-data>

<string name="facebook_app_id">14734343</string>

Add Facebook Login Button to Activity Layout

You need to add below login button to layouts of all activities where facebook login button needs to be displayed.


Register FacebookCallback with Facebook Login Button

You need to register FacebookCallback with login button in onCreate() method of facebook login activity. For that you need to first create CallbackManager and implement FacebookCallback class’s onSuccess, onCancel and onError callback methods and then call registerCallback on facebook login button.

On clicking facebook login button, intent is fired to start the facebook login process. In order to get results from the facebook login process, you need to override onActivityResult method in your activity and call CallbackManager’s onActivityResult() method.

CallbackManager intern calls appropriate call back method on FacebookCallback which is registered to facebook login button.

 private CallbackManager callbackManager;
private LoginButton fbLoginButton;

protected void onCreate(Bundle savedInstanceState) {

    callbackManager = CallbackManager.Factory.create();

    fbLoginButton = (LoginButton) findViewById(R.id.fb_login_button);

    fbLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        public void onSuccess(final LoginResult loginResult) {

                    "login success",


        public void onCancel() {
                    "login cancel",

        public void onError(final FacebookException exception) {
                    "login failed",
protected void onActivityResult(final int requestCode,
                                final int resultCode,
                                final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);

Facebook Logout in Android App

Facebook login button will be converted into facebook logout button on successful login.

facebook android login logout

Retrieving and Displaying Facebook Profile

After successful facebook login, you can get and display facebook user profile information in your android app using Profile object. The user profile information which you can access depends on what access permissions were requested for while logging in.

To display user facebook profile picture, you can use ProfilePictureView widget in UI.


And set profile id when you need to display picture.

profilePictureView = (ProfilePictureView) findViewById(R.id.fb_user_picture);
userNameView = (TextView) findViewById(R.id.fb_user_name);

final Button permsButton = (Button) findViewById(R.id.fb_picture_button);

permsButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(final View v) {
        Profile profile = Profile.getCurrentProfile();
        if (profile != null) {
                    .setText(String.format("%s %s",profile.getFirstName(), profile.getLastName()));

Android Facebook Login Requesting Permissions

You can request additional read, publish and audience permissions by setting permission on login button.

To request for audience permissions, you can use setDefaultAudience () method of login button. To request for publish permissions, method setPublishPermissions can be used and for setting read permission, use setReadPermissions() method.

ArrayList<String> publish = new ArrayList<>();


Android Facebook Login Remove Permissions

You can give users of your android app an option to remove all facebook permissions, which are granted to your app, using GraphRequest object and sending delete request to me/permissions. You need to provide GraphRequest.Callback object which implements onCompleted method to get the response.

        GraphRequest.Callback callback = new GraphRequest.Callback() {
            public void onCompleted(GraphResponse response) {
                try {
                    if(response.getError() != null) {
                        //error in removing permissions
                    else if (response.getJSONObject().getBoolean(SUCCESS)) {
                } catch (JSONException ex) {  }
        GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(),
                "me/permissions", new Bundle(), HttpMethod.DELETE, callback);