Android Facebook Login Integration Example Tutorial Eclipse

How to connect-add Facebook login inside android application so app user can login using Facebook account.

Facebook is one of the most usable major social networking website available on internet and similar to Google+ it also gives us the facility to integrate Facebook Login in our existing android application using Facebook SDK + API integration. So any application developer who wish to gives more accessibility to its app users can add Facebook login inside their android application so there is no need to create new account each times when new user came. User who wish to see your app content can easily login with its existing Facebook account. So here is the complete step by step tutorial for Android Facebook Login Integration Example Tutorial Eclipse.

Android Facebook Login Integration Example Tutorial Eclipse

Please follow these Steps to integrate Facebook Login in your android application :

android-project-download-code-button

1. Generate key hash for Facebook.

2. Create Facebook App ID using Facebook Developers.

To create Facebook application app id Login with your Facebook account into Facebook developers. Then Click on Create a New App -> Enter your app Display Name, Enter Namespace, Enter Contact Email, Choose category ->Click on Create App id Button.

3. Click on Choose a Platform button.

choose-a-platform

4. Select Platform as Android.

5. Now follow all the steps.

6. After following all the step it will ask you about hash key so enter the hash key and select Add Facebook Login button.

7. Now download Facebook SDK from git repositories.

8. Now extract the downloaded Facebook SDK and import this SDK into your Eclipse.

9. To import SDK into Eclipse GoTO -> File -> Import -> Android -> Existing code into Workspace.

Note : Please import only Facebook project inside SDK Folder.

10. Right click on your Facebook SDK project ->Properties -> Android -> Library -> Check is Library.


is-library

11. Now right click on your Login project -> Properties -> Add -> Library -> Add -> Select Facebook sdk project as library.

facebook-sdk

Here you go now all the import process is done, Now its time to start coding.

Android Facebook Login Integration Example Tutorial Eclipse source code.

Please add internet permission inside your AndroidManifest.xml file .

<uses-permission android:name="android.permission.INTERNET"/>

Please add meta data and theme data inside your AndroidManifest.xml file.

 <activity android:name="com.facebook.LoginActivity"
 android:theme="@android:style/Theme.Translucent.NoTitleBar" />
 
 <meta-data android:name="com.facebook.sdk.ApplicationId" 
 android:value="@string/facebook_app_id" />

Code for MainActivity.java file.

package com.facebooklogin_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.facebook.widget.LoginButton.UserInfoChangedCallback;
import java.util.Arrays;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 private TextView DisplayUserName;
 private LoginButton login_button ;
 private UiLifecycleHelper uilifecyclehelper;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 uilifecyclehelper = new UiLifecycleHelper(MainActivity.this, SessionStatusCallBack);
 
 uilifecyclehelper.onCreate(savedInstanceState);

 setContentView(R.layout.activity_main);

 DisplayUserName = (TextView) findViewById(R.id.fb_username);
 
 login_button = (LoginButton) findViewById(R.id.login_button);
 
 login_button.setReadPermissions(Arrays.asList("email"));
 
 login_button.setUserInfoChangedCallback(new UserInfoChangedCallback() {
 
 @Override
 public void onUserInfoFetched(GraphUser user) {
 
 if (user != null) {
 
 DisplayUserName.setVisibility(View.VISIBLE);
 
 DisplayUserName.setText("Logged in UserName = " + user.getName());
 
 } else {
 
 DisplayUserName.setText("Please log in.");
 }
 }
 });
 }
 
 private Session.StatusCallback SessionStatusCallBack = new Session.StatusCallback() {
 
 @Override
 public void call(Session session, SessionState state,
 
 Exception exception) {
 
 if (state.isOpened()) {
 
 Log.d("MainActivity", "Session Start.");
 
 } else if (state.isClosed()) {
 
 Log.d("MainActivity", "Session Close.");
 }
 }
 };
 
 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 uilifecyclehelper.onActivityResult(requestCode, resultCode, data);
 }
 
 @Override
 public void onSaveInstanceState(Bundle savedState) {
 super.onSaveInstanceState(savedState);
 uilifecyclehelper.onSaveInstanceState(savedState);
 }
 
 @Override
 public void onPause() {
 super.onPause();
 uilifecyclehelper.onPause();
 }
 
 @Override
 public void onResume() {
 super.onResume();
 uilifecyclehelper.onResume();
 }

 @Override
 public void onDestroy() {
 super.onDestroy();
 uilifecyclehelper.onDestroy();
 }
 
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.facebooklogin_android_examples.com.MainActivity" >

 <com.facebook.widget.LoginButton
 android:id="@+id/login_button"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true" />

 <TextView
 android:id="@+id/fb_username"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/login_button"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="47dp"
 android:text="demo"
 android:textSize="18dp"
 android:gravity="center"
 android:visibility="gone" />
 
</RelativeLayout>

Code for AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.facebooklogin_android_examples.com"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="8"
 android:targetSdkVersion="21" />
 
 <uses-permission android:name="android.permission.INTERNET"/>

 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 
 <activity android:name="com.facebook.LoginActivity"
 android:theme="@android:style/Theme.Translucent.NoTitleBar" />
 
 <meta-data android:name="com.facebook.sdk.ApplicationId" 
 android:value="@string/facebook_app_id" />
 
 </application>

</manifest>

Screenshots:

Android Facebook Login Integration Example Tutorial Eclipse

fb-login-2

Click here to download Android Facebook Login Integration Example Tutorial Eclipse project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *