Android - Share custom link via facebook

10,081

In order to share the something in Facebook better go with latest Facebook SDK. It will make your task simpler. Because Android Intent has its own restrictions when we use share to facebook. Refer my answer below regarding this

Share text via Intent on Facebook without using Facebook sdk

The screenshot that you posted seems to have the sharing of link from the App.

Here is how you will integrate the Facebook SDK to your project.

Facebook SDK integration

Then use the following code to share link on the facebook.

ShareDialog shareDialog;
FacebookSdk.sdkInitialize(Activity.this);
shareDialog = new ShareDialog(act);
ShareLinkContent linkContent = new ShareLinkContent.Builder()
                    .setContentTitle("title")
                    .setContentDescription(
                            "Description")
                    .setContentUrl(Uri.parse("your url")).build();
            shareDialog.show(linkContent);

More sharing options from Facebook can be found here, which is pretty simple and straightforward.

Happy coding..!!

Share:
10,081
Mina Fawzy
Author by

Mina Fawzy

This Mina I am working as Android & iOS developer for more than three years , I love help people , challenge myself #SOreadytohelp

Updated on June 04, 2022

Comments

  • Mina Fawzy
    Mina Fawzy almost 2 years

    I need to share specific part in my app when user open it , if he download my app it navigate direct to this part (it may be nested fragment).

    String AppURL = "https://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName();
    String urlToShare = "http://stackoverflow.com/questions/7545254"; // I need custom url to specific part in my app 
    
    // See if official Facebook app is found
    boolean facebookAppFound = false;
    List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0);
    for (ResolveInfo info : matches) {
        if (info.activityInfo.packageName.toLowerCase().startsWith("com.facebook.katana")) {
            intent.setPackage(info.activityInfo.packageName);
            facebookAppFound = true;
            break;
        }
    }
    
    // As fallback, launch sharer.php in a browser
    if (!facebookAppFound) {
        String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
    }else{
    
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    // intent.putExtra(Intent.EXTRA_SUBJECT, "Foo bar"); // NB: has no effect!
    intent.putExtra(Intent.EXTRA_TEXT, urlToShare);
    }
    
    startActivity(intent);
    

    here is what I need to approach

    enter image description here

  • Mina Fawzy
    Mina Fawzy about 8 years
    can I reference activity in my app ? instead of url
  • Swaminathan V
    Swaminathan V about 8 years
    do you want to show your exact page from your app and post it to facebook ?
  • Mina Fawzy
    Mina Fawzy about 8 years
    yea exactly , when he click at link it navigate to store to download app if not installed , if install open certain page
  • Swaminathan V
    Swaminathan V about 8 years
    then add your play store link in the contentdescription field and share. It will do what you wanted.
  • Bhavin Patel
    Bhavin Patel over 7 years
    Sharing link is works but issue is that while click on shared link in FB app it should redirect to new page but instead of that it will show error message like "There's problem opening this app". thanks in advance