Authentication

Facebook Login for Android Easy Tutorial [With Video]

Pinterest LinkedIn Tumblr



Facebook has over 2400 million users, and it makes sense to implement Facebook login in your Android app for faster and better user experience.

Facebook Users 2020

Users have become utterly impatience. They want everything instant either to watch a video on Youtube or signing an app.

I don’t want to waste your time, let’s get started.

How Does It Work? (Video)

Facebook Login for Android Step by Step Guide

In this tutorial, you’re going to create a simple login system with Facebook.

We also wrote a tutorial on Android Facebook Login with Firebase Auth that was done in Android Studio.

This tutorial is without Android Studio. Cool?

1. Select an App or Create a New App

First of all, go to the Facebook Developer Console. Select an older app if you have or create a new app.

create new app for facebook

Now a popup box appears Enter Display Name, Contact Email and create a new app Id.

create app id

Then Facebook might be asking you to fill the captcha. Just fill and hit Enter.

Now you can see the app is generated.

2. Download the Facebook App

Now if you don’t have Facebook installed on your device. Just install it from Google Play Store.

By the way, downloading the app is not necessary.

Mobile Apps Category (English)300x250

But if you didn’t download the app. It will redirect to the browser.

3. Create/Open a Project

Now open an existing project or create a new Android Studio Project

4. Integrate the Facebook SDK

Facebook login SDK is a component of Facebook SDK For Android. To use Login SDK into your project. You have to add a maven repository.
  • Just go to the app -> Gradle Scripts -> build.Gradle(project) and add below code inside buildScript { repositories {} }; and click sync Now.
  • Now go to the app -> Gradle Scripts -> build.Gradle(app) and paste below code inside dependencies{} block and click Sync now.

5. Add Files in strings.xml

Open your app/res/values/strings.xml File and add below code inside <resources> Tag.

You can find your app key, and protocol key at Facebook developer console in section 4.

6. Add Files in AndroidManifest.xml

Open the app/manifests/AndroidManifest.xml file and add below code inside <manifest> TAG.

Now paste below metadata element, Facebook activity, customTabActivity inside <application> TAG

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

Now go to the developer console of Facebook and go to part 5. Now enter package name, default activity class name (This is a class where your login functionality performs) and click save.
Add package Name and class name

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

If you want to generate a release key you need two things.

    1. 1. Key and certificate management tool (key tool) from the java development kit
  1. 2. open SSL for the window you can get it from Google code archive

And paste the below code in the cmd terminal.

You will get a 28 character key just like the below image.

key

And paste inside Facebook developer terminal section 6 and click save

key in facebook console

9. Add the Facebook Login Button

Now add below code inside activity_main.xml

10. Do Some Code in MainActivity.java

First of declare these fields, in the beginning, MainActivity class.

Now create a callback method just to handle login response. Just copy below code and paste inside MainActivity onCreate() method.

Now register a callback method in your onCreate() method.
If you are adding a button in the fragment then you have to update your activity to use the fragment. You can put these properties in the onCreateView() if you are using a fragment.
After registering the callBack method you can get all the profile information using GraphRequest. This will gives you a JSON format information of the logged in the user. Then you can filter this information and use this inside your app. That’s it. pretty simple 🙂
If the login Button works fine then LoginResult parameter has new accessToken also have most recent permission that is granted or declined.
Finally, in your onActivityResult() method, just call callbackManager.onActivityResult() to pass the login results to LoginManager.
Here is the complete code of MainActivity.java

11. Run the app

That’s it. Now just run the app. Below you can see all profile information of your User Account.

Profile info in logcat

This StackOverflow article is really helpful you can check this if you are getting any issue.

Or ask any question you might have, our experienced Android developers will answer each of your questions.

Advertisements
W3Schools
Author

Hi there! I am Founder at FirebaseTutorials and AndroidBull. My skills include Android App Development. If you have any idea that you would want me to develop? Let’s talk: waqas@firebasetutorials.com