julika Zen
julika Zen

Reputation: 357

android facebook like

I want to integrate a like button in my android app. I used the code

likeWebView = (WebView) findViewById( R.id.webView1 );
 likeWebView.getSettings().setJavaScriptEnabled(true);
 String url = "http://www.facebook.com/plugins/like.php?" +
           "href=" + URLEncoder.encode("likeurl" ) + "&" +
           "layout=standard&" +
           "show_faces=false&" +
           "width=500&" +
           "action=like&" +
           "colorscheme=light&" +
           "access_token=" + URLEncoder.encode( "read_stream" );
likeWebView.loadUrl( url );

But after login it is showing a blank page. Please give me a solution to add a Like button.

Upvotes: 6

Views: 6987

Answers (3)

Yogesh Lakhotia
Yogesh Lakhotia

Reputation: 888

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 enter image description here

Upvotes: 8

Tyler Wall
Tyler Wall

Reputation: 3788

A lot of the answers work; I am just building on top of what is already above. In actually implementing their solution I found that the device's default browser would open because the shouldOverrideUrlLoading was not being set.

Also, I changed it from recommended to like (I know little things like that can piss people off if they're looking for copy & paste code - I've gotten ticked at that :P).

The following is code that is working as of July 25th 2012.

THIS IS ONLY THE LIKE BUTTON change the width/height if you want the other stuff in the webView.


Java

    WebView likeWebView = new WebView(this);
    likeWebView.getSettings().setJavaScriptEnabled(true);

            likeWebView.setWebViewClient(new WebViewClient() {
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            view.setVisibility(View.INVISIBLE);
        }

        public void onPageFinished(WebView view, String url) {
            view.setVisibility(View.VISIBLE);
        }

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }


    });

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

    likeWebView.loadUrl(url);

Layout

<WebView
    android:id="@+id/fb_likeWebView"
    android:layout_width="51dp"
    android:layout_height="24dp" />

Cheers! :D

Upvotes: 0

Horrorgoogle
Horrorgoogle

Reputation: 7868

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

Upvotes: 2

Related Questions