Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • FaceBook Integration in Android Application - 8 Steps Process

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 2.68k
    Comment on it

    Facebook integration helps the user to login to your app with his/her Facebook account.

     

     

    If you are looking for the procedure to integrate Facebook in your android then do follow the steps mentioned below:-

     

    Step 1) In your project build.gradel(project) add the repository to the buildscript {repositories{}} section

    mavenCentral()

    like shown below

    buildscript {
        repositories {
            jcenter()
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.3.0'
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            jcenter()
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }

     

    Step 2) Now in your project build.gradel(module:app) add below mentioned compile line to dependencies{}

    compile 'com.facebook.android:facebook-android-sdk:[4,5)'

    after adding these lines sync your project.

     

    Step 3) Now edit your manifest file by adding Internet permission and meta-data tag and activity for facebook as shown below

    <uses-permission android:name="android.permission.INTERNET"/>
    <meta-data android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id"/>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.facebook.CustomTabActivity"
        android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="@string/fb_login_protocol_scheme" />
    </intent-filter>
    </activity>

     

    Step 4) Now you need to associate your package name and default activity class name so that the facebook authenticate your app to use facebook login etc, for this please go to the link

    https://developers.facebook.com/docs/facebook-login/android

     

    Step 5) Now to ensure the authenticity for interaction between facebook and our app we need to provide development and release key Hash for our app to facebook, so to generate the key Hash on mac run below command on android studio terminal

    keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

    and for windows run the command given below

    keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl base64

    now add the generated key Hash to facebook portal, for this use the same link that is mentioned above in 4th step.

     

    Step 6) After all above work done the facebook will generate a facebook id and fb login protocol scheme, add both of these in your string.xml file as shown here

    <string name="facebook_app_id">1927074434217758</string>
        <string name="fb_login_protocol_scheme">fb1927074434217758</string>

     

    Step 7) Now add the facebook login button in your app using the below code in your activity layout file

    <com.facebook.login.widget.LoginButton
        android:layout_margin="30dp"
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        />

     

    Step 8) Now for the functioning of this facebook button add the following code in your activity java file where we can customize the login button properties

    private LoginButton loginButton;
    private CallbackManager callbackManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        loginButton = (LoginButton)findViewById(R.id.login_button);
        loginButton.setReadPermissions("email");
        // If using in a fragment
        // Other app specific specialization
        callbackManager = CallbackManager.Factory.create();
        // Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                Toast.makeText(MainActivity.this, "Logged in Successfully", Toast.LENGTH_SHORT).show();
                Log.e("Logged in","Logged in successfully");
            }
            @Override
            public void onCancel() {
                // App code
            }
            @Override
            public void onError(FacebookException exception) {
                Log.e("Logged in","Error");
                // App code
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
    FaceBook Integration in Android Application - 8 Steps Process

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: