How to use Facebook like button in Android application?
Asked Answered
E

2

1

I am using the following code to integrate the LIKE functionality in facebook.com with my Android app.

First: I used an html file called FacebookLikeView.html and I placed it in "assets/www/" folder, and this is what I used in it:

<html xmlns:fb="http://ogp.me/ns/fb#">
<body>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src ="//connect.facebook.net/en_GB/all.js#xfbml=1&appId=My APP ID";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
</body>
<fb:like href="https://www.facebook.com/pages/OmarTalkcom/52242794400"    send="true" width="120" show_faces="true" font="arial">
</fb:like>
</html>

and then I used a button to launch the html file in a webView like this:

   Button like = (Button) findViewById(R.id.button1);
    like.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            WebView myWebView = (WebView) findViewById(R.id.webView1);
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            myWebView.loadUrl("file:///android_asset/www/FacebookLikeView.html");
        }
    });

But nothing happend and in the log I got the following when I click the button:

 04-28 09:33:56.940: D/CONTEXT(31684): nativeDraw called here 
 04-28 09:33:56.940: D/CONTEXT(31684): PictureSet* draw called here 
 04-28 09:33:56.940: V/(31684): In ResourceHandle::create newHandle
 04-28 09:33:56.990: D/dalvikvm(31684): GC_CONCURRENT freed 103K, 4% free     7195K/7431K, paused 8ms+3ms
 04-28 09:33:56.990: D/CONTEXT(31684): nativeDraw called here 
 04-28 09:33:56.990: D/CONTEXT(31684): PictureSet* draw called here 
 04-28 09:33:57.080: D/CONTEXT(31684): nativeDraw called here 
 04-28 09:33:57.080: D/CONTEXT(31684): PictureSet* draw called here 

So what am I doing wrong here or what is the right thing to do in order to like a facebook page from Android app ?

Note I put the above html code in html file and opened it with chrome and firefox but it didn't work, But it worked when I copied it to the following link in w3school html sample.

Eosinophil answered 28/4, 2013 at 6:38 Comment(2)
i know it's been so long, but have you found any solution for this. if yes then please post...Shoring
check this post, there is well-formed library to solve this issue: https://mcmap.net/q/664379/-how-to-add-to-my-android-application-a-button-than-do-like-to-a-facebook-pageEthiopic
S
1

Go throw with following sample example ...

https://github.com/crowjdh/android-facebook-like-button-integration

Try native facebook like button

https://developers.facebook.com/docs/android/like-button

Reference https://developers.facebook.com/docs/reference/android/current/class/LikeView

Salient answered 29/4, 2013 at 5:56 Comment(3)
I'll check it now, but would you please check my EDIT to the question at the bottom in the Note section?Eosinophil
you missed adding "http:", connect.facebook.net/en_GB/…Salient
It didn't work, and the github project is not the same behavior that I want in my appEosinophil
L
1
<html xmlns:fb="http://ogp.me/ns/fb#">
<body>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src ="https://connect.facebook.net/en_US/all.js#xfbml=1&appId=your app id";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
</body>
<fb:like href="https://www.facebook.com/pages/OmarTalkcom/52242794400"    send="true" width="120" show_faces="true" font="arial">
</fb:like>
</html>

Note:You have to fill your facebook app id and I changed the js.src to https://connect.facebook.net/en_US/all.js

Lillia answered 19/4, 2014 at 12:29 Comment(1)
It is properly working in my app with simple webViewLillia

© 2022 - 2024 — McMap. All rights reserved.