简体   繁体   中英

Custom Facebook Share-Button, Android App

So I want to implement a custom Facebook share button into my Android app, but thus far I've only managed to use the native one, which I imported into my .xml file and made use of in my Java activity for that specific page.

My code looks like this (in .xml);

<com.facebook.share.widget.ShareButton
            android:id="@+id/share_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="Share"
            android:layout_centerHorizontal="true"/>

And in my .java, outside onCreate();

private ShareButton shareButton;

ShareLinkContent content = new ShareLinkContent.Builder()
        .setContentUrl(Uri.parse("https://developers.facebook.com"))
        .setContentTitle("MyTitle")
        .build();

Inside onCreate();

    shareButton = (ShareButton)findViewById(R.id.share_btn);
    shareButton.setShareContent(content);

How would I go about making use of a custom button that I've imported into XML? Using .setShareContent obviously doesn't work if it's not an instance of ShareButton. Thanks in advance!

I have found a solution. It's clear that facebook share will work on its own button click event. So you will have to explicitly call the View.performclick() method.

Here is how i have done this:

In my layout

<com.facebook.share.widget.ShareButton
        android:id="@+id/share_btn_fb"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        android:contentDescription="@string/share" />

    <LinearLayout
        android:id="@+id/share_btn"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:orientation="horizontal"
        android:gravity="center_vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:src="@drawable/google_share" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/share_google"
            android:textColor="@color/green_color"
            android:layout_marginLeft="5dp"
            android:textSize="18sp" />
    </LinearLayout>

Inside activity get refrences of these views.

  ShareButton  shareButton = (ShareButton) layout.findViewById(R.id.share_btn_fb);
  LinearLayout shareFB = (LinearLayout) layout.findViewById(R.id.share_btn);
  LinearLayout shareGoogle = (LinearLayout) layout.findViewById(R.id.share_google);
  shareGoogle.setOnClickListener(this);
  shareFB.setOnClickListener(this);

Inside onClick()

case R.id.share_btn :
                shareButton.performClick();
                break;
            case R.id.share_btn_fb :
                ShareLinkContent content = new ShareLinkContent.Builder()
                        .setContentTitle("Content title")
                        .setContentDescription("App descr")
                        .setContentUrl(Uri.parse("some url"))
                        .build();
                shareButton.setShareContent(content);
                break;

Basically shareButton.performClick(); is doing the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM