Facebook Like button in Android Application

心已入冬 提交于 2019-11-28 20:55:13

Facebook SDK hasn't provided like button feature for native mobile apps( As of Oct 17 2011). It is available in Mobile Web apps. For more info you can check out these link:
Mobile - Facebook Developers
Like Button - Facebook Developers

The like option can be implemented easily on Native platform languages(Android, iOS) as well as browsers (curl, PHP, JavaScript) as follows. Go to developer.facebook app section and within the Open Graph section of the Developer App configuration, add the built-in Like action, which should appear in the drop-down when adding a new Open Graph action. Refer here for latest updates.

Once done, Select "Get Code" option to retrieve sample code as depicted below for Android platform. You can choose platform of your choice. Note that app_specific_unique_identifier is uniquee for apps, i have removed it for security reasons and you have to use the one for your app.

I have been able to test the Like flow successfully. Hope this helps.

Bundle params = new Bundle();
params.putString("object", "http://samples.ogp.me/<app_specific_unique_identifier>");

Request request = new Request(
    Session.getActiveSession(),
    "me/og.likes",
    params,
    HttpMethod.POST
);
Response response = request.executeAndWait();
// handle the response

Finally Facebook and Launched Like Button for Android

Steps:

1 - Add Facebook Library to Project

2 - Create App on Facebook 3 - Update Manifest

**In the Application tab add meta-data**

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

4 - Add LikeView in Layout

//activitymain.xml
<com.facebook.widget.LikeView
            android:id="@+id/like_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
        </com.facebook.widget.LikeView>

5 - ActivityMain.java

//set facebook page or link to this like button
LikeView likeView;
UiLifecycleHelper uiHelper;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activitymain);
    uiHelper = new UiLifecycleHelper(this, null);
    likeView = (LikeView) findViewById(R.id.like_view);
    likeView.setObjectId("https://www.facebook.com/<page_username>");//it can be any link

    likeView.setLikeViewStyle(LikeView.Style.STANDARD);
    likeView.setAuxiliaryViewPosition(LikeView.AuxiliaryViewPosition.INLINE);
    likeView.setHorizontalAlignment(LikeView.HorizontalAlignment.LEFT);

}

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

    uiHelper.onActivityResult(requestCode, resultCode, data, null);

}

Output

You can use facebook like button in android app using special permission from facebook developer account https://developers.facebook.com. Add your app here and submit app special permission. Go to app review and submit items for approval. click on start submission and after that select LIKE native button and submit all details they want like why you want get like permission , how your app will use this permission everything. If Facebook will approve your request then you can use facebook like button inside app.enter code here

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

After this you need to do some java code.

likeView = (LikeView) findViewById(R.id.facebooklike);
    likeView.setLikeViewStyle(LikeView.Style.STANDARD);            
   likeView.setAuxiliaryViewPosition(LikeView.AuxiliaryViewPosition.INLINE);
    likeView.setHorizontalAlignment(LikeView.HorizontalAlignment.CENTER);
    likeView.setObjectIdAndType("url of like page", LikeView.ObjectType.PAGE);

like functionality automatically call when you click on like button.

now you need to get response from like page that user like that page of unlike that page.

enter code here

public class FbLikes extends AppCompatActivity {

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        if (resultCode == RESULT_OK) {
            // verify we're returning from like action
                // get action results
                bundle = data.getExtras().getBundle("com.facebook.platform.protocol.RESULT_ARGS");
                if (bundle != null) {
                    like = bundle.getBoolean("object_is_liked");// liked/unliked
                    bundle.getInt("didComplete");
                    bundle.getInt("like_count"); // object like count
                    bundle.getString("like_count_string");
                    bundle.getString("social_sentence");
                    bundle.getString("completionGesture"); // liked/cancel/unliked
                    Log.e(TAG, bundle.getString("social_sentence") + "");
                    Log.e(TAG, "likeornot" + bundle.getBoolean("object_is_liked") + "");
                    Log.e(TAG, "lcomplete" + bundle.getString("completionGesture") + "");
                    Log.e(TAG, "count" + bundle.getInt("like_count") + "");
                    Log.e(TAG, "countstr" + bundle.getString("like_count_string") + "");
                    Log.e(TAG, "did" + bundle.getInt("didComplete") + "");
                }
        }
    } catch (Exception e) {

    }
}

} this code will return everything you want from like functionality.

Try this one i made some modification

String url = "http://www.facebook.com/plugins/like.php?layout=standard&show_faces=true&width=80&height=50&action=like&colorscheme=light&href=http://beta.demo.dy/web/service/304.htm";
                 //   webview = (WebView) findViewById(R.id.webview);
                    webView.loadUrl(url);
                     webView.setWebViewClient(new WebViewClient());
                 class LikeWebviewClient  extends WebViewClient
                    {
                        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                            view.loadUrl(url);
                            return true;
                        }
                    }

Facebook has recently provided the feature of likebutton in facebook sdk 3.21.1. You can go through these links to download sdk and tutorial for implementation of likebutton.

Download SDK: http://developers.facebook.com/docs/android

Tutorial http://developers.facebook.com/docs/android/like-button

I hope it may solve your problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!