Android allows you to connect your android app to facebook. You can connect your app to facebook and share data or any other updates on facebook. By connecting app to facebook you can use facebook login, account kit, share and send dialog, app event, graph API, etc. In this tutorial you will learn to integrate facebook in android app using Android Studio and add login using facebook SDK.

The Facebook SDK is very easy to integrate in your Android app with Facebook. There are two different ways to integrate facebook and share something from your app – Facebook SDK and Intent Share.

Releted:
Integrating Google Firebase Analytics to your Android Project
How to Integrate Google AdMob in Android
Google Maps Android API Getting Started Tutorial : Part 1

Android Example: Integration Facebook Login in Android Using Android Studio


Let’s start by creating new android project using Android Studio or you can use any other development environment like eclipse to integrate facebook login in android app/game. Create new project with project name: Facebook Integration Android and minimum API 15: Android 4.0.3.

Facebook Integration in Android - Facebook Login


Create New Facebook App


Open facebook developer site developers.facebook.com . Open MyApp tab and click on the Add a New App. Here you need to fill up app information like name, contact email, app category etc. and click Create App Id button.


In Product Setup (Add Product) page click Facebook Login Get Started button. Enable Client OAuth Login, Web OAuth Login, Embedded Browser OAuth Login, Force Web OAuth Reauthentication and click Save Changes button.

From fb app dashboard click Choose Platform button and select Android.


Generate Android Hash Key


Now you need to get key hash value for your app. The key hash value is used by facebook as security check for login. To get hash key for your app, follow the following steps:

1. Open command prompt (Terminal)
2. Run the following command:
keytool -exportcert -alias androiddebugkey -keystore C:\Program Files\Java\jdk1.8.0_25\bin\debug.keystore | C:\OpenSSL\bin\openssl sha1 -binary | C:\OpenSSL\bin\openssl base64

Make sure you have installed jdk and OpenSSL in your PC and replace C:\Program Files\Java\jdk1.8.0_25\bin with your jdk bin directory and C:\OpenSSL\bin with your OpenSSL bin directory.


Now you will get hash key something like 2jmj7w0yVb/vlkK/YBwk= copy it.

Go to facebook developer page and select your app. From setting section click Add Platform and choose Android.

Here you need to provide more information about app like app package name, class name, hash key that we have generated above and so on. Add these information, enable Single Sign On and click Save Changes button.

Google Play Package Name: com.viralandroid.facebookandroidintegration
Class Name: com.viralandroid.facebookandroidintegration.MainActivity
Key Hashes: 2jmj7w0yVb/vlkK/YBwk=

From App Review section enable Make Android Integration public.

Add Repositories and Compile Dependency


Open module build.gradle file from android studio. Add repositories {mavenCentral()} and compile 'com.facebook.android:facebook-android-sdk:[4,5)' as dependency and build your project. Build.gradle file will look like below.
Module:build.gradle


Add Facebook App ID


Open your app strings.xml file, /app/src/main/res/values/strings.xml. Add a new string with the name facebook_app_id containing the value of your Facebook App ID. Strings.xml file will look like below.

res/values/strings.xml


Update your Android Manifest File


Open AnddroidManifest.xml file and add a user-permission and meta-data element. Android manifest file will look like this.

AndroidManifest.xml


Android XML Layout File


Open activity_main.xml file and add a TextView and facebook LoginButton widget. This XML layout file will look like this.

res/layout/activity_main.xml



Modify Java Activity File


In your java activity file, add callbackManager and onActivityResult. Following is the complete content of java activity file.

src/MainActivity.java


Now run your application by clicking Run icon from toolbar. Make sure you have connected your android device with computer and internet. Above are the screenshots of this example project.

Post a Comment

Powered by Blogger.