My First Android Facebook Social App

In this post, I am going to discuss about creating very basic Android application that interacts with Facebook using Facebook SDK.

What is Facebook SDK?

Facebook provides SDK for Android to build Android applications to increase the engagement with it’s World’s number one Social Networking platform. To know more about Facebook SDK, please follow this link.

What are we going to develop?

The application which we are going to develop will allow you to get the name of the Facebook User (Mostly your name) and display it inside our application.

You can download source code from here if you don’t want to create Application from scratch, otherwise please proceed with below listings.


Facebook SDK must be configured in your Eclipse IDE, please take help from this post – Getting Started with the Facebook SDK for Android to setup Facebook SDK.

Let us start with creating Android application.

Step 1: Create Android Application Project

  • Create new android project [File >> New >> Android Application Project] with project name FBBasicActivity
  • Enter the package name and activity name you have entered in Facebook developer console while creating Facebook Application
  • Choose Minimum Required SDK, Target SDK and Compile with (Choose latest SDKs to make sure Facebook SDK will work without any issues). Confused on choosing these options? Take a look at Minimum Required SDK – Target SDK – Compile With post.
  • Click Next button and finally click ‘Finish’ to create project

Step 2: Add Facebook SDK as dependency project

Right click on the project you created >> Choose Properties >> Choose Android Tab >> Go to lower part of the dialog >> Click on Add button >> Select FacebookSDK Project.

Troubleshooting Tips:

  1. You should configure FacebookSDK in your workspace, that means import FacebookSDK library project into your workspace before your try to add it as dependency project.
  2. FacebookSDK library project and the Anroid application you just created should be under same Workspace.

Step 3: Design screen

Open activity_main.xml and replace it with below code.

<RelativeLayout xmlns:android=""
    tools:context="com.prgguru.example.MainActivity" >

        android:text="@string/hello_world" />


Step 4: – Main Class

Open and replace it with below code.

Each line of code is commented well, if you still have any doubt or question discuss it.

package com.prgguru.example;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.prgguru.example.R;

public class MainActivity extends Activity {

	public void onCreate(Bundle savedInstanceState) {

		// start Facebook Login
		Session.openActiveSession(this, true, new Session.StatusCallback() {

			// callback when session changes state
			public void call(Session session, SessionState state,
					Exception exception) {
				if (session.isOpened()) {

					// make request to the /me API
							new Request.GraphUserCallback() {

								// callback after Graph API response with user
								// object
								public void onCompleted(GraphUser user,
										Response response) {
									// when user is not null
									if (user != null) {
										// get user's name and set it in TextView
										TextView welcome = (TextView) findViewById(;
										welcome.setText("Hello "
												+ user.getName() + "!");

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Session.getActiveSession().onActivityResult(this, requestCode,
				resultCode, data);


Step 6: Add the Facebook app ID into your project

Add your Facebook app ID into your project, open up the strings.xml file in the res/values folder of your project and add below value:

<string name="app_id">YOUR_FACEBOOK_APP_ID</string>

Step 7: Add Meta Data to application tag in AndroidManifest

As shown below, add a ‘Meta Data’ item called com.facebook.sdk.ApplicationId, with a value of the app_id string you just configured:

            android:value="@string/app_id" />

Step 8: Add permission in AndroidManifest.xml

Add Internet permission in AndroidManifest:

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

Step 9: Add Facebook Login Activity in AndroidManifest.xml

            android:theme="@style/AppTheme" >


Here is the complete AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" />
        android:theme="@style/AppTheme" >
            android:value="@string/app_id" />

            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            android:theme="@style/AppTheme" >



That’s all. It’s time to test our code.

Run the application using emulator or device by right clicking on the project >> Run as >> Android applicaiton >> Choose emulator or device.

Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project (How to import android project in eclipse).

Download Source Code

*apk in Android  is the installation file similar to exe in windows.


If you feel this article is helpful and interesting please spread a word about it to your friends and colleagues by sharing the article in Facebook or Twitter.


You are always welcome to provide your comments and feedback from comment box.



Related Articles

Author: Udhay

Share This Post On
468 ad
Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest