Please Wait 10 Second

How To Setup Facebook Login Option On Android Apps – Android Studio Tutorial

Step 1 – Facebook Set up

To start using Facebook functions, you need to:

  1. Go to the Facebook Developer Website and create an account and an app.
  2. Follow the Facebook’s Quickstart Guide by clicking here and pay attention to the following recommendations:
  3. Here are the steps included in the Facebook’s Quickstart Guide which you need to follow carefully, as you are not going to follow them exactly like Facebook suggests:
    • In Step 3, instead of adding
implementation '[4,5)'
  1. in the dependencies{} section at build.gradle (Module:app), add the following code in the dependencies{} section at build.gradle (Module:app) (if the implementation is giving errors, try replacing it with compile):
1 2 3// update the versions to the latest ones implementation ‘’ implementation ‘’
  1. Remember to update the version of Parse Facebook Utils SDK for Android to the latest one. You can find out which is the latest version at JitPack website, following these steps:
      1. At JitPack website paste parse-community/FacebookUtils-SDK-Android in the Git repo url box.
      2. After doing that, click on the Look up button. Then you should see the available versions of Parse SDK for Android, as shown in the following image.
    • In Step 4, you’ll be asked to add internet permission at application element in /app/manifest/AndroidManifest.xml file, but you have already added it while following Install Parse SDK Tutorial, so you don’t need to do this.
    • In Step 6, you’ll need to provide key hashes and to do that you must have Open SSL installed. Facebook’s Guide doesn’t provide command lines to generate key hashes in Linux, but doing that is simple, as all it requires from you is to open a terminal window and run the following command:
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
  1. Don’t follow the steps of Facebook’s Quickstart Guide right after Step 6.The steps not mentioned above should be followed exactly according to the Facebook’s Quickstart Guide.
  1. Go to your App dashboard at Creativeliku Website and click on Server Settings.
  2. Find the “Facebook Login” block and click on Settings. The “Facebook Login” block looks like this:
  3. Go back to your Android Studio Project, open your strings file: .../app/src/main/res/values/strings.xml, copy your facebook_app_id and paste it in the Facebook appId field of the last page of Back4App that you opened. Lastly, press the + sign and click on save button.

Step 3 – Add provider element in the Manifest file

Go back to your Android Studio Project and inside the application element in /app/manifest/AndroidManifest.xml file, right after the meta data element, add the following code.

   1 <provider
   2 android:name="com.facebook.FacebookContentProvider"
   3 <!--don't forget to put your Facebook App ID in the         following link-->
   4 android:authorities=""
    5 android:exported="true" />

Don’t forget to put your Facebook App ID in the code above.

Step 4 – Initialize Parse Facebook Util SDKs

In your Android Studio Project, in the Java file called App that extends Application that you created to initialize the Parse SDK, on its onCreate method, right after Parse.initialize() call, use the following code to initialize Parse Facebook Utils SDK.

1 ParseFacebookUtils.initialize(this);

If you don’t have an file as described in this step, access the Install Parse SDK for Android documentation and be sure that you have followed all the steps required to install Parse SDK correctly, otherwise, your facebook login may not work properly.

Step 5 – Set up

At the beginning of each Parse activity, import the following:

// Front End Dependencies
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.parse.Parse;
import com.parse.ParseUser;

Step 6 – Log In

To implement your Login Activity, do the following:

  1. Import into your LoginActivity, in addition to the dependencies imported in Step 4
  2. :
import android.util.Log;
import android.widget.Toast;

import com.parse.facebook.ParseFacebookUtils;

import com.facebook.AccessToken;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;

import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.SaveCallback;
  1. To implement Facebook Login, simply use the following code
Collection<String> permissions = Arrays.asList("public_profile", "email");
ParseFacebookUtils.logInWithReadPermissionsInBackground(LoginActivity.this, permissions, new LogInCallback() {
    public void done(ParseUser user, ParseException err) {
        if (err != null) {
            Log.e("err", "err", err);
        if (user == null) {
             Toast.makeText(LoginActivity.this, "The user cancelled the Facebook login.", Toast.LENGTH_LONG).show();
             Log.d("MyApp", "Uh oh. The user cancelled the Facebook login.");
        } else if (user.isNew()) {
             Toast.makeText(LoginActivity.this, "User signed up and logged in through Facebook.", Toast.LENGTH_LONG).show();

             Log.d("MyApp", "User signed up and logged in through Facebook!");
       } else {
            Toast.makeText(LoginActivity.this, "User logged in through Facebook.", Toast.LENGTH_LONG).show();

            Log.d("MyApp", "User logged in through Facebook!");
  1. In the example project, this code is placed inside a LOGIN VIA FACEBOOK button callback.
  2. As you can see, there are many more methods included in the code above. The getUserDetailFromFB method is responsible for fetching user details. Here’s the code for this method:
void getUserDetailFromFB(){
         GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),new  GraphRequest.GraphJSONObjectCallback(){
            public void onCompleted(JSONObject object, GraphResponse response) {
                ParseUser user = ParseUser.getCurrentUser();
                }catch(JSONException e){
                }catch(JSONException e){
                user.saveInBackground(new SaveCallback() {
                    public void done(ParseException e) {
                        alertDisplayer("First Time Login", "Welcome!");
        Bundle parameters = new Bundle();
  1. It’s interesting to add an additional method to display Alert Dialogs and make the process look more professional. The method below does this:
private void alertDisplayer(String title,String message){
         AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // don't forget to change the line below with the names of your Activities
                        Intent intent = new Intent(LoginActivity.this, LogoutActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        AlertDialog ok = builder.create();;
  1. It’s very important to use the following as a method outside the onCreate() method of your LoginActivity to pass login results to the LoginManager via callbackManager and avoid errors.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       ParseFacebookUtils.onActivityResult(requestCode, resultCode, data);

Step 6 – Log out

To implement Facebook Logout, simply use the code mentioned below:

// logging out
alertDisplayer("So, you're going...", "Ok...Bye-bye then");

In the example project, this code is placed inside a LOG OUT button callback.

The method alertDisplayer is the same that you added in the LoginActivity, don’t forget to change its Intent arguments though.

It’s done!

Adblock Detected

Please disable adblock to proceed to the destination page