Search code examples
androidfacebook-share

com.facebook.share.widget.ShareButton is disabled in Android App


I have a Facebook Share button in my Android App. When I used it with ImageButton or ImageView onClick, it was working perfectly fine in debug mode. But APK Build was getting failed with release type.

I fixed this issue with the help of Stackoverflow Question but ended up in having FB Share button being disabled.

Tried adding the below code as advised by a few answers here but still not working.

shareButton.setShareContent(linkContent);

Below is my XML from where I am calling onClick function.

<com.facebook.share.widget.ShareButton
    android:id="@+id/imgFBShare"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/lblChangePasswordTitle"
    android:layout_alignEnd="@+id/scrollView2"
    android:layout_marginBottom="12dp"
    android:onClick="shareFB"
    app:srcCompat="@drawable/com_facebook_button_icon_blue" />

Below is my onClick "shareFB" code.

public void shareFB (View view) {
        callbackManager = CallbackManager.Factory.create();

        List<String> permissionNeeds = Arrays.asList("publish_actions");

        //this loginManager helps you eliminate adding a LoginButton to your UI
        loginManager = LoginManager.getInstance();

        loginManager.logInWithPublishPermissions(this, permissionNeeds);

        if (ShareDialog.canShow(ShareLinkContent.class)) {
            Bitmap image = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
            Uri imageUri = Uri.parse(main_image_url);

            ShareButton shareButton = (ShareButton) findViewById(R.id.imgFBShare);

            ShareLinkContent linkContent = new ShareLinkContent.Builder()
                    .setContentTitle(short_headline)
                    .setContentDescription(
                            short_description)
                    .setContentUrl(Uri.parse("http://mywebsite.com/android-app-download/"))
                    .setImageUrl(imageUri)
                    .build();

            shareButton.setShareContent(linkContent);

            shareDialog.show(linkContent);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int responseCode, Intent data)
    {
        super.onActivityResult(requestCode, responseCode, data);
        callbackManager.onActivityResult(requestCode, responseCode, data);
    }

Am I missing anything?


Solution

  • After going through every single information in detail, I managed to find out the solution by two very simple steps.

    1. Instead of <com.facebook.share.widget.ShareButton in my Activity.xml, I reverted to <ImageView. This gives further flexibility to define own image.
    2. From my shareFB I removed ShareButton shareButton = (ShareButton) findViewById(R.id.imgFBShare); and shareButton.setShareContent(linkContent);

    Now it works perfectly fine!