How to Add to My Android Application a Button Than Do Like to a Facebook Page

Android :: Facebook like button

Get the id of the post that you want to like and store it in fbPostId.

Then, inside the onClickHandler of your like button, put the following code snippet :

Request likeRequest = new Request(Session.getActiveSession(), fbPostId + "/likes", null, HttpMethod.POST, new Request.Callback() {

@Override
public void onCompleted(Response response) {
Log.i(TAG, response.toString());
}
});
Request.executeBatchAndWait(likeRequest);

Facebook Like Button in Android app?

The Like button can be used to like a Facebook Page or any Open Graph object and can be referenced by URL or ID. Here's what the code looks like.
documentation android facebook sdk
In your Activity or Fragment's onCreate method, use either the UiLifecycleHelper or call Settings.sdkInitialize:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

uiHelper = new UiLifecycleHelper(this, callback);
// if you don't want to use the UiLifecycleHelper, call sdkInitialize instead
// Settings.sdkInitialize(this);
...

Then set the object ID for the Like button (this can be a URL or a Facebook ID):

LikeView likeView = (LikeView) findViewById(R.id.like_view);
likeView.setObjectId("http://shareitexampleapp.parseapp.com/photo1/");

Lastly, call the UiLifecycleHelper again in your onActivityResult method

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

uiHelper.onActivityResult(requestCode, resultCode, data, null);
// if you don't use the UiLifecycleHelper, call handleOnActivityResult on the LikeView instead
// LikeView.handleOnActivityResult(this, requestCode, resultCode, data);
...

Sample Image

Implement custom Facebook like button instead of using Facebook's Default Like Button(LikeView)?

Because it's Android you can customize absolutely everything. For example I have created sample custom LikeView. You can set any color you want but try to consider Facebook convention.

Sample ImageSample Image

How to do?

Rule as following:

In Android application if you reimplement resource from any SDK it overides it to new value which you set.

I have found these variables from FacebookSdk classes and has overided it on my own application. You can do too.

1. Add following style to your style file and set your own selector. It will overide LikeButton selector

<style name="com_facebook_button_like" parent="com_facebook_button">
<item name="android:background">@drawable/btn_facebook_like_background_selector</item>
</style>

2.Add following colors to your color.xml file. Set any color you want.

<color name="com_facebook_likeboxcountview_text_color">#FFFFFFFF</color>
<color name="com_facebook_likeboxcountview_border_color">#FFFFFFFF</color>

Android WebView for Facebook Like Button

To get past the blank page you do this:

 webview.setWebViewClient(new LikeWebviewClient(this));

private class LikeWebviewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
Log.d(TAG, "onPageFinished url: " +url);
// Facebook redirects to this url once a user has logged in, this is a blank page so we override this
// http://www.facebook.com/connect/connect_to_external_page_widget_loggedin.php?............
if(url.startsWith("http://www.facebook.com/connect/connect_to_external_page_widget_loggedin.php")){
String redirectUrl = getFacebookLikeUrl();
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}

Facebook Like button redirecting to facebook site in android

My guess is that the webview in which you load the fb js sdk just does not have the cookies and so the user is not authenticated.

How are you authenticating the user? is it using SSO (that is, is the fb app installed?) if that's the case then the browser (webview) is not aware that the user is authenticated and when you click it it just tries to redirect you for authentication.

Read the new official blog post: Bringing Like to Mobile.


Edit

Well, this looks exactly as I guessed. You see that it says "Sign up to see..." meaning that the js sdk does not recognize that the user is logged in and authenticated.

You have two options that I can think of:

1. As I wrote use the new Like action and create your own "like button".

2. When calling FB.init pass the authResponse parameter with what you have from android, will look something like:

Java part

m_cObjFacebook = new Facebook("Your_id");
m_cObjFacebook.authorize(this, m_cPermissions, new DialogListener() {
@Override
public void onComplete(Bundle values) {
String response = m_cObjFacebook.request("me");
JSONObject json = Util.parseJson(response);
showWebView(json.getString("id"));
}

....
});

private void showWebView(String userid) {
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

StringBuilder url = new StringBuilder("file:///android_asset/FacebookLikeView.html?");
url.append("token=").append(cObjFacebook.getAccessToken());
url.append("&expires=").append(cObjFacebook.getAccessExpires());
url.append("&user=").append(userid);

mWebView.loadUrl(url.toString());
}

Html / Javascript part:

<script type="text/javascript">
window.fbAsyncInit = function() {
var data = {},
query = window.location.search.substring(1);

query = query.split("&");
for (var i = 0; i < query.length; i++) {
var pair = query[i].split("=");
data[pair[0]] = pair[1];
}

FB.init({
appId: "YOUR_APP_ID",
xfbml: true,
authResponse: data
});
};

// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
<div class="fb-like" data-href="http://www.facebook.com/FacIntegra" data-send="false" data-width="450" data-show-faces="false" data-font="tahoma"></div>

I'm not 100% sure that this trick will work, since one of the parameters in the authResponse is the signedRequest which you don't have, but it's worth a try.


2nd Edit

When the facebook application (katana) exists on the device then the authentication is done using it, which means that the cookies on the default browser are not created and so when you open a webview with the like button the js sdk is not aware you are logged in, you can think of it like logging into facebook on firefox, and then open a facebook on chrome and see that you are not logged in.

When the fb app is not installed the sdk authenticate the user using a dialog with a webview, which creates the cookies that can be later used by the js sdk, that's why it works "as expected" in this scenario.

The workaround I gave you in my 1st edit tries to pass the authentication data to the sdk when it's being initialized.

As I wrote then, I'm not sure it will work, maybe you also need to pass it a signed request aswell, but since you don't have it you'll need to create it, if you want to try that just do the exact opposite of what's described here: Singed Request, and then pass it to the FB.init along with the access token and expire parameters.

Another option you have is to always use the dialog for authentication, for that read this: How to disable Facebook single sign on for android - Facebook-android-sdk.

I advise against using that method since it results in bad user experience, after all typing email and password on mobile devices is not a fun thing to do.

integrate facebook with like button in android and iphone

There is no like button in facebook graph api. There are some alternatives that you can select. First you can use a webview and show the like button in a webview.

https://developers.facebook.com/docs/reference/plugins/like/

Another alternative is using facebook share functionality in facebook-android-sdk.

Last and more general alternative is using an intent and let the user select how to share it. (it can be any app including facebook)

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "YOUR SUBJECT HERE!");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "YOUR TEXT HERE");
startActivity(Intent.createChooser(shareIntent, "YOUR TITLE HERE"));

android facebook like

private initLikeButton( String urlToLike ) {
likeWebView = (WebView) findViewById( R.id.likeWebView );
likeWebView.getSettings().setJavaScriptEnabled(true);

String url = "http://www.facebook.com/plugins/like.php?" +
"href=" + URLEncoder.encode( urlToLike ) + "&" +
"layout=standard&" +
"show_faces=false&" +
"width=375&" +
"action=recommend&" +
"colorscheme=light&" +
"access_token=" + URLEncoder.encode( FacebookAdapter.getInstance().getAccessToken() );

likeWebView.loadUrl( url );

Here, in your code you must be put likeurl , url that like.

using the iframe code provided by the like button code generator at http://developers.facebook.com/docs/reference/plugins/like. But since an iframe is basically the same thing as a WebView, it seemed redundant to load the code in an iframe and then load the iframe in a WebView. So instead I just loaded the code that would be in the iframe directly into the WebView using the code below. But the same thing happens either way.

Incidentally, the same issue exists when developing an iPhone app. We don't want the user to have to login to facebook every time they run our app. But unfortunately, if the user has logged into facebook on another computer since the last time they logged into facebook via our app, they'll have to log in again.

On the android platform I would think a better solution might be to have a facebook app that you send an intent to that takes care of keeping the user logged in and returning the html for rendering the like button.

see more: https://github.com/facebook/facebook-android-sdk/issues/17

http://blog.doityourselfandroid.com/2011/02/28/30-minute-guide-integrating-facebook-android-application/

http://www.integratingstuff.com/2010/10/14/integrating-facebook-into-an-android-application/

integrate facebook with like button in android and iphone



Related Topics



Leave a reply



Submit