Search code examples
androidfacebooksharefacebook-sdk-4.x

Share button looks disabled


I'm trying to use a share button in my application, but it looks as if it was disabled:

disabled share button

I already initialize the sdk:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FacebookSdk.sdkInitialize(getApplicationContext());
}

I also have the manifest correctly configured like the documentation suggest: https://developers.facebook.com/docs/android/getting-started

The button in the view is like this:

<com.facebook.share.widget.ShareButton
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="@string/share"
              android:id="@+id/share_on_fb_button"
              bind:onClick="shareOnFBButtonClick"/>

What could be the problem? Why is the button disabled? Should I implement login functionality in my app? Or I only need to have facebook application on the device?


Solution

  • See com.facebook.share.widget.ShareButtonBase:

    /**
     * Sets the share content on the button.
     * @param shareContent The share content.
     */
    public void setShareContent(final ShareContent shareContent) {
        this.shareContent = shareContent;
        if (!enabledExplicitlySet) {
            internalSetEnabled(canShare());
        }
    }
    

    Calling setShareContent to enable the shareButton, try it:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_main);
    
        ShareButton fbShareButton = (ShareButton) findViewById(R.id.share_on_fb_button);
        ShareLinkContent content = new ShareLinkContent.Builder()
                .setContentUrl(Uri.parse("https://developers.facebook.com"))
                .build();
        fbShareButton.setShareContent(content);
    }
    

    Note: https://developers.facebook.com/docs/sharing/android

    "When you implement sharing, your app should not pre-fill any content to be shared. This is inconsistent with Facebook Platform Policy, see Facebook Platform Policy, 2.3."