Facebook Like button in Android Application

28,396

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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.

Share:
28,396
Megha
Author by

Megha

Updated on July 09, 2022

Comments

  • Megha
    Megha almost 2 years

    I want to display like button in my android application. Below is the code I use to display a Like button in my android application.

    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.webView1);
    webview.loadUrl(url);
    webview.setWebViewClient(new LikeWebviewClient());
    
    public class LikeWebviewClient  extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    

    But when I run this application it displays a white area. How to resolve this?

  • Megha
    Megha over 12 years
    i want to dispaly that in Webview of android.it is possible in ios they also disaply webview of like button so i want to that
  • overbet13
    overbet13 almost 11 years
    this is a great answer. I've been searching for such an answer for three days in a row. Thanks a lot!
  • Karim Varela
    Karim Varela over 9 years
    I don't have the option for a built-in (common) like action. Any suggestions?
  • Thomas Vos
    Thomas Vos almost 8 years
    Is there a way to like the application itself? Just like the Google plus one button in Google Play.