Updated 14 December 2016
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.
There are two ways to implement Facebook login on Android:
1 2 3 4 5 6 7 |
<com.facebook.login.widget.LoginButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="5dp" facebook:com_facebook_confirm_logout="false" facebook:com_facebook_tooltip_mode="never_display" /> |
There are other classes you use to add login to your app. The SDK includes:
LoginManager
. So when someone clicks on the button, the login is initiated with the set permissions. The button follows the login state, and displays the correct text based on someone’s authentication state.onActivityResult
call.
Sample Activity to implement login using Facebook Android SDK.
1. Add Compile dependency in build.gradle:
dependencies {
        compile ‘com.facebook.android:facebook-android-sdk:4.+’
}
2. Initialize  FacebookSdk
and setup facebook’s CallbackManager
If you use the LoginButton
in a fragment, you need to set the fragment on the button as shown by calling setFragment
.
You then need to call FacebookSdk.initialize
to initialize the SDK, and then call CallbackManager.Factory.create
to create a callback manager to handle login responses. Here’s an example of adding the callback in a fragment:
1 2 |
FacebookSdk.sdkInitialize(getApplicationContext()); callbackManager = CallbackManager.Factory.create(); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(this.getApplicationContext()); callbackManager = CallbackManager.Factory.create(); LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { boolean enableButtons = AccessToken.getCurrentAccessToken() != null; Profile profile = Profile.getCurrentProfile(); if (enableButtons && profile != null) { profilePictureView.setProfileId(profile.getId()); greeting.setText(getString(R.string.hello_user, profile.getFirstName())); } else { profilePictureView.setProfileId(null); greeting.setText(null); } } @Override public void onCancel() { // App code } @Override public void onError(FacebookException exception) { // App code } }); setContentView(R.layout.main); profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture); greeting = (TextView) findViewById(R.id.greeting); } |
Layout used in the activity: main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:facebook="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFF" android:orientation="vertical"> <LinearLayout android:id="@+id/main_ui_container" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFF" android:orientation="vertical"> <com.facebook.login.widget.LoginButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="5dp" facebook:com_facebook_confirm_logout="false" facebook:com_facebook_tooltip_mode="never_display" /> <LinearLayout android:layout_width="150dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:id="@+id/greeting" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="10dp" android:textColor="#333" android:textSize="18sp" /> <com.facebook.login.widget.ProfilePictureView android:id="@+id/profilePicture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:gravity="center_horizontal" facebook:com_facebook_preset_size="normal" /> </LinearLayout> </LinearLayout> </LinearLayout> |
4. On successfull Login
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()
:
1 2 3 4 5 |
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); } |
Note: Every activity and fragment that you integrate with the FacebookSDK Login or Share should forward onActivityResult
to the callbackManager.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.