This tutorial explains how to how to use Facebook SDK APIs in Android. This tutorial allows user to login to Facebook first and then allows to do following things

  1. Login to Facebook
  2. Share status message to Facebook wall
  3. Share Image to Facebook wall

This example using Facebook SDK 3.0 for Android which is available for free download from the Facebook’s developer console. This Facebook SDK comes with almost all the functionality of the native Facebook app to your own Android app. Follow the below steps

Creating an App on Facebook

Before integrating Facebook SDK to your android app you have to create a Facebook app on your Facebook developer console. Visit Facebook developer console and create a new Facebook application. Once you create the application you will get an app id, which is used to uniquely distinguish your application from others. This AppID will be used along with each of the request we make to Facebook server. Don’t get panic, sometime it takes some time to make your application go live.

Using Facebook SDK in Android

  1. Now you have created an Facebook app in developer console and you are ready to start with our Android app. You may create a new project or import an existing one.
  2. Import Facebook SDK to your eclipse workspace and make sure it builds successfully. Once it builds we have to attach the Facebook library project to your Android project. The easiest way to attach the SDK is to add it as an Android library by going to the project’s properties.
  3. Now do the following changes to your application manifest file. Put the following code before </application> tag. Make sure you are using your own AppID obtained from Facebook developer console.
<activity android:name="com.facebook.LoginActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

<meta-data android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />

Note: Make sure that you have give android.permission.INTERNET permission in your application manifest file.

Activity Layout

Now define layout for your activity class. In this example, We have three sample buttons.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:padding="20dp" >

        facebook:fetch_user_info="true" />

        android:textSize="18sp" />

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

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


Activity Java Class

public class FBActivity extends FragmentActivity {
	private LoginButton loginBtn;
	private Button postImageBtn;
	private Button updateStatusBtn;

	private TextView userName;
	private UiLifecycleHelper uiHelper;
	private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
	private static String message = "Sample status posted from android app";

	public void onCreate(Bundle savedInstanceState) {

		uiHelper = new UiLifecycleHelper(this, statusCallback);

		userName = (TextView) findViewById(;
		loginBtn = (LoginButton) findViewById(;
		loginBtn.setUserInfoChangedCallback(new UserInfoChangedCallback() {
			public void onUserInfoFetched(GraphUser user) {
				if (user != null) {
					userName.setText("Hello, " + user.getName());
				} else {
					userName.setText("You are not logged");

		postImageBtn = (Button) findViewById(;
		postImageBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View view) {

		updateStatusBtn = (Button) findViewById(;
		updateStatusBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

	private Session.StatusCallback statusCallback = new Session.StatusCallback() {
		public void call(Session session, SessionState state, Exception exception) {
			if (state.isOpened()) {
				Log.d("FacebookSampleActivity", "Facebook session opened");
			} else if (state.isClosed()) {
				Log.d("FacebookSampleActivity", "Facebook session closed");

	public void buttonsEnabled(boolean isEnabled) {

	public void postImage() {
		if (checkPermissions()) {
			Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
			Request uploadRequest = Request.newUploadPhotoRequest(
					Session.getActiveSession(), img, new Request.Callback() {
						public void onCompleted(Response response) {
									"Photo uploaded successfully",
		} else {

	public void postStatusMessage() {
		if (checkPermissions()) {
			Request request = Request.newStatusUpdateRequest(
					Session.getActiveSession(), message,
					new Request.Callback() {
						public void onCompleted(Response response) {
							if (response.getError() == null)
										"Status updated successfully",
		} else {

	public boolean checkPermissions() {
		Session s = Session.getActiveSession();
		if (s != null) {
			return s.getPermissions().contains("publish_actions");
		} else
			return false;

	public void requestPermissions() {
		Session s = Session.getActiveSession();
		if (s != null)
			s.requestNewPublishPermissions(new Session.NewPermissionsRequest(
					this, PERMISSIONS));

	public void onResume() {

	public void onPause() {

	public void onDestroy() {

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		uiHelper.onActivityResult(requestCode, resultCode, data);

	public void onSaveInstanceState(Bundle savedState) {



Facebook Android Example

This article is about: Facebook, Facebook oAuth, Facebook SDK,


She is self-motivated enthusiastic techie spending most of her time in writing code. She loves coding, android and java. She has developed her software personality with understanding on various tools and technologies and hands on experience on app development.


Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.
  • Older version of Facebook v 3.2

  • Vinoth Vino

    Facebook SDK version?

  • Ashutosh


  • HiMangi PatEl

    plss share source code for this demo

    • Vaghela Divyrajsinh

      BUs Copy Copy Or Kuch Nahi……………………….This Example Is Done, So u Try Completly Work

  • Tech Morphosis

    Superb dude .. I used ur Twitter and facebook code , everything works great :)
    How do I get email ID from facebook API ?

  • Androd Mangalore

    where is source code

  • Androd Mangalore

    how to download shource code

  • saurabh kalani

    how to integrate facebook page with first i want to display my facebook page

  • Pratik Gondil

    I have an error “no library found for com.facebook.Request”

  • Manikandan Kuppusamy

    thanks for posting but i am not able to get any data from user.. user returns null value how to fix this ???

  • Sam

    perfect tutorial friend, thanks

  • Manish Raaz

    com.facebook.widget.loginbutton error in xml showing n also r cannot be resolved to a variable android

    • com.facebook.widget.LoginButton has been changed to com.facebook.login.widget.LoginButton in version 4 of Facebook SDK. Using that should fix the error with both xml and R.

  • Krishna Pillai

    Hi Can you please explain the role of UILifeCycleHelper in this code ? Why is it used ?

  • Devendra Sharma

    hi i have one problem get.
    only one user first login than image share.
    but second user login and than image are not post.
    plz help me……

  • devendra

    hi m devendra sharma android developer.
    i have one problem get use in this code.
    only one fb id ragister and another id login in this code than they are login but share an image. so plz help me.

  • what is the error ?

  • Nilanchala Panigrahy

    Facebook SDK 2.9

  • Priya

    I am getting the error NullPointerException: Argument ‘applicationId’ cannot be null

    • Nilanchala Panigrahy

      Did you create a app in Facebook developer console?

  • Nitesh Kumar

    Nice one. Thanks.

  • Nilanchala Panigrahy

    Looks like some issue with the Facebook sdks. At runtime the sdks is not found. Rebuild or restart eclipse.

  • Randy Rowell

    Thank you. This is 10 times better than the official facebook documentation.

  • Don’t have the example but you can refer the api document here

  • libin

    unfortunately stopped in my device

    • Please do paste the error log. Difficult to trace without it.

  • Vikram Arora

    thanks a lot

  • André Luiz Teste

    your application worked on my phone but did not work on the phone from my brother, when I click the login button and does not open the facebook page which asks permission to access and post.

  • Thirumalai

    Hi very nice tutorial.But update status and post image function is not working.It ask me again to login.When Im done it does nothing.Kindly help me.Thanks in Advance.

  • James Cosgrave

    Is this discussion still active ?

  • abhai

    the username is not getting displayed in my case. i checked everything.

  • Bincy

    Thanks…….i want to share url on facebook wall.. can you post me the code to do it

  • Aakash Gupta

    Thanks for the code. I want to access email ID too..
    Whenever i try user.getProperty(“email”); my application crashes. I add email permission in List PERMISSIONS = Arrays.asList(“publish_actions”,”email”);
    Can you tell what i m doing wrong?

  • bidhu

    Hi it allowed me to login only once and that to i couldn’t post any status update or upload any image.

  • Download the sdk. It comes with sample that fetching profile pic from fb.

  • amr khamis

    worked like charm :D thank you ;)
    where can i get the token so that i post photos in other activities ??!!

  • Uzair Majid

    can someone please explain the working of this code?
    some methods are pretty confusing ?

  • Risk Rømer

    can this be achieved without fragments? Would like to reach earlier devices to Level 11. Thanks!

    • @Risk, very much doable with simple activity.

  • Sami

    Life Saver !

  • Mike Howard

    Perfect example… I was messing around on the Facebook dev pages for a few hours and didn’t get very far. This sample exactly what I was looking for.

  • Swaminathan

    code works finely…i wanna share url with image on facebook wall.. can you post me the code to do it

  • harsha

    but where the user login details taken for new user

  • Macy Chan

    i can only run the App successfully once even I commented the sentence “buttonsEnabled(false); “. how can i do if i want to send several status by clicking the button for several times?

    • Deveti Putnik

      Same here…