Facebook SDK Integration in Android Tutorial Part 2 (Sign in with facebook)

August 11, 2014
To add an option call sign in with facebook we need to follow following procedure:

First we need to add facebook app id to our project.
-->To add the Facebook app ID into your project, open up the strings.xml file in the res/values folder of your project. we need to add an app_id string containing the ID that we obtained  from the https://developers.facebook.com website . To know how to create facebook app id you can read part1.

strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">SampleFBIntegration</string>
    <string name="hello_world">Hello world!</string>
    <string name="app_id">739274269466534</string>

</resources>


Next add Internet permission , meta data tag and Activity in AndroidManifest.xml as follows:

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ram.samplefintegration"
    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>

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


        <activity android:name="com.facebook.LoginActivity" />
    </application>

</manifest>


Next create login button and one TextView in xml  layout as follows:

activity_main.xml:
<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"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:text="Login" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button_login"
        android:layout_marginTop="27dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>





Next add following java code in Activity as follows:
MainActivity.java:
package com.ram.samplefintegration;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.Session.StatusCallback;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;

public class MainActivity extends Activity implements OnClickListener
{
    Button loginButton;
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        text = (TextView) findViewById(R.id.textView);
        loginButton = (Button) findViewById(R.id.button_login);
        loginButton.setOnClickListener(this);

    }

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

    @Override
    public void onClick(View v)
    {
        // start facebook login
        Session.openActiveSession(this, true, new StatusCallback()
        {
            // callback when session changes state
            @Override
            public void call(Session session, SessionState state, Exception exception)
            {
                if (session.isOpened())
                {
                    // make request to the /me API to get user details
                    Request.newMeRequest(session, new Request.GraphUserCallback()
                    {

                        // callback after Graph API response with user
                        // object
                        @Override
                        public void onCompleted(GraphUser user, Response response)
                        {
                            if (user != null)
                            {
                                // To display facebook user name of the user
                                text.setText("Hello " + user.getName() + "!");
                            }
                        }
                    }).executeAsync();
                }

            }
        });

    }
}







Output Screen Shots:







Artikel Terkait

Previous
Next Post »