android facebook integration android studio example

      Comments Off on android facebook integration android studio example

android facebook integration android studio example

android facebook integration android studio example

Android facebook integration android studio example contains step by step guide to integrate facebook into Android studio project. Just follow steps and integrate facebook login and sharing in 10 minutes. You need following with you to start with Facebook integration in your Android Apps.

Step 1 : Goto https://developers.facebook.com and create a new app. Here most important is package name and key hash (You need to create key hash and update it)

Make sure to add contact email address (It’s compulsory else your app will not work)

Then Goto App Review and Make your App Online (This is must to use app in android)

Step 2 : Download Facebook SDK and copy only facebook folder into your project folder (where your app folder is there)

Now add that facebook into settings.gradle where include: app is already there… you need to add another line which is your facebook sdk

include ‘:app’
include ‘:facebook’

Step 3 : Setup minimum SDK to 15 or above

Step 4 : Following into your Android manifest.xml

Make sure to replace XXXXXXXXXXXXX with your facebook app id

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

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

   <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>

 <provider android:authorities="com.facebook.app.FacebookContentProviderXXXXXXXXXXXXXXX"
            android:name="com.facebook.FacebookContentProvider"
            android:exported="true"/>


 </application>

 

Step 5 : Add following into your string.xml

Make sure to replace XXXXXXXXXXXXX with your facebook app id

<string name="facebook_app_id">XXXXXXXXXXXX</string>
    <string name="fb_login_protocol_scheme">fbXXXXXXXXXXXXX</string>

 

Step 6 : Code to add Code for Login Button

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.facebook.login.widget.ProfilePictureView;
import com.facebook.share.model.ShareHashtag;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.widget.ShareDialog;

import org.json.JSONException;
import org.json.JSONObject;

public class FacebookIntegration extends AppCompatActivity {

    CallbackManager callbackManager;
    Button share,details;
    ShareDialog shareDialog;
    LoginButton login;
    ProfilePictureView profile;
    Dialog details_dialog;
    TextView details_txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_facebook_integration);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        callbackManager = CallbackManager.Factory.create();
        login = (LoginButton)findViewById(R.id.login_button);
        profile = (ProfilePictureView)findViewById(R.id.picture);
        shareDialog = new ShareDialog(this);
        share = (Button)findViewById(R.id.share);
        details = (Button)findViewById(R.id.details);
        login.setReadPermissions("public_profile email");
        share.setVisibility(View.INVISIBLE);
        details.setVisibility(View.INVISIBLE);
        details_dialog = new Dialog(this);
        details_dialog.setContentView(R.layout.dialogdetails);
        details_dialog.setTitle("Details");
        details_txt = (TextView)details_dialog.findViewById(R.id.details);
        details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                details_dialog.show();
            }
        });

        if(AccessToken.getCurrentAccessToken() != null){
            RequestData();
            share.setVisibility(View.VISIBLE);
            details.setVisibility(View.VISIBLE);
        }
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(AccessToken.getCurrentAccessToken() != null) {
                    share.setVisibility(View.INVISIBLE);
                    details.setVisibility(View.INVISIBLE);
                    profile.setProfileId(null);
                }
            }
        });

        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (ShareDialog.canShow(ShareLinkContent.class)) {
                    ShareLinkContent linkContent = new ShareLinkContent.Builder()
                            .setContentTitle("Hello Facebook")
                            .setContentDescription(
                                    "The 'Hello Facebook' sample  showcases simple from Adarsh Patel")
                            .setContentUrl(Uri.parse("http://adarshspatel.in"))
                            .build();

                    shareDialog.show(linkContent);
                }

              



            }
        });
        login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

                if(AccessToken.getCurrentAccessToken() != null){
                    RequestData();

                    share.setVisibility(View.VISIBLE);
                   details.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException exception) {
            }
        });


    }



    public void RequestData(){
        GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject object, GraphResponse response) {

                JSONObject json = response.getJSONObject();
                try {
                    if(json != null){
                        String text = "<b>Name :</b> "+json.getString("name")+"<br><br><b>Email :</b> "+json.getString("email")+"<br><br><b>Profile link :</b> "+json.getString("link");
                        details_txt.setText(Html.fromHtml(text));
                        profile.setProfileId(json.getString("id"));
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,link,email,picture");
        request.setParameters(parameters);
        request.executeAsync();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode,resultCode,data);
    }

}

 

Step 7 : XML Code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="FacebookIntegration"
    tools:showIn="@layout/activity_facebook_integration">


    <com.facebook.login.widget.ProfilePictureView
        android:id="@+id/picture"
        android:layout_gravity="center"
        android:layout_width="200sp"
        android:layout_height="200sp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="200sp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="91dp"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/details"
        android:layout_alignStart="@+id/details" />
    <Button
        android:id="@+id/details"
        android:text="Show details"
        android:layout_gravity="center"
        android:layout_width="200sp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/share"
        android:layout_alignLeft="@+id/share"
        android:layout_alignStart="@+id/share" />
    <Button
        android:id="@+id/share"
        android:text="Share"
        android:layout_gravity="center"
        android:layout_width="200sp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/picture"
        android:layout_alignLeft="@+id/picture"
        android:layout_alignStart="@+id/picture" />

</RelativeLayout>

 

 

 

Save

Save

Save