Implement Facebook like and share in Android App


Implement Facebook API:

Prerequisities:

This tutorial using Android Studio.

* goto https://developers.facebook.com/

*Click on My Apps, and Click on "Add New App".

*Click on "Android" to select your Platform.

*Then follow the instruction

*To get the KeyHashes:
run from commandLine:

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

Steps:

1.
In build.gradle,add the latest facebook sdk
compile 'com.facebook.android:facebook-android-sdk:[4,5)'

and tap on Sync Now.


2.
Add your facebook app id in
res->values->strings.xml
,which you get from Register your app in facebook developer account

3.
In AndroidManifest.xml, add permission,

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

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


add provider and facebook activity , {appid} add your appid here:

 <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:label="@string/app_name" />

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


4.
Like and share to facebook from your application,
it will like or share to the Facebook application(if installed),
else open a webview auto.

Create a xml, have the likeview and sharebutton

<com.facebook.share.widget.LikeView
        android:id="@+id/likeView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />


    <com.facebook.share.widget.ShareButton
        android:id="@+id/shareButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />



5.
For Like,

 CallbackManager callbackManager;
 LikeView likeView;


in onCreate(),
 likeView = (LikeView) findViewById(R.id.likeView);
 likeView.setLikeViewStyle(LikeView.Style.BOX_COUNT);
 likeView.setAuxiliaryViewPosition(LikeView.AuxiliaryViewPosition.INLINE);


 
        likeView.setObjectIdAndType(
                "add your link which you want to share",
                LikeView.ObjectType.PAGE);



For Share,

    ShareButton shareButton;

in onCreate(),
shareButton=(ShareButton)findViewById(R.id.shareButton);

        ShareLinkContent content = new ShareLinkContent.Builder()
                .setContentUrl(Uri.parse("add your link which you want to share"))
                .build();
        shareButton.setShareContent(content);



and then add onActivityResult() on your page as:

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



Enjoy, when user will like and share the content right from your app.


Enjoy Android Coding,

Comments