61

Is there anything similar to getsharekit.com for Android? It allows to share URL's to social networking sites. Is there anything similar to this or do I need to code separately for facebook, Twitter and email?

Jonik
  • 80,077
  • 70
  • 264
  • 372
sunil
  • 9,541
  • 18
  • 66
  • 88
  • 1
    Somewhat better answers for each case can be found in these questions: [Facebook](http://stackoverflow.com/questions/7545254/android-and-facebook-share-intent), [Twitter](http://stackoverflow.com/questions/2077008/android-intent-for-twitter-application), [email](http://stackoverflow.com/questions/8701634/send-email-intent) (include the URL in email body using `Intent.EXTRA_TEXT`). – Jonik Apr 10 '14 at 19:55
  • This is my solution which is working fine just for sharing URL on Facebook, http://stackoverflow.com/a/29529335/513413 – Hesam Apr 09 '15 at 03:43

7 Answers7

89

I don't know if that's what you mean but you can use the Android built-in sharing menu...

You can share a URL to Facebook, Twitter, Gmail and more (as long as the apps are installed on your device) using Intents:

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Sharing URL");
i.putExtra(Intent.EXTRA_TEXT, "http://www.url.com");
startActivity(Intent.createChooser(i, "Share URL"));

If the app you want to share to is not installed on the user's device, for example - facebook, then you'll have to use Facebook SDK.

If you want your Activity to handle text data shared from other apps as well, you can add this to your AndroidManifest.xml:

<activity android:name=".ShareLink">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

Hope this helps!

Ziem
  • 6,579
  • 8
  • 53
  • 86
Lior Iluz
  • 26,213
  • 16
  • 65
  • 114
47

You can use also ShareCompat class from support library.

ShareCompat.IntentBuilder(context)
    .setType("text/plain")
    .setChooserTitle("Share URL")
    .setText("http://www.url.com")
    .startChooser();

https://developer.android.com/reference/androidx/core/app/ShareCompat

lukjar
  • 7,220
  • 2
  • 31
  • 40
  • 1
    Now just use androidx https://developer.android.com/reference/androidx/core/app/ShareCompat but implementation is the same – mikep Apr 14 '21 at 11:52
6

For facebook you can use `

https://m.facebook.com/sharer.php?u=website_url&t=titleOfThePost

website url could be any thing refereing to any resource for example if you want to get an image from internet and sharung it on your wall .

hope this would help

Muhannad A.Alhariri
  • 3,702
  • 4
  • 30
  • 46
4

// for URL

Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
share.putExtra(Intent.EXTRA_TEXT, "http://www.codeofaninja.com");
startActivity(Intent.createChooser(share, "Share link!"));

// for image

Intent share = new Intent(Intent.ACTION_SEND);
// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");

// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
        + "/myImage.png";

File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
Ashish Chaugule
  • 1,526
  • 11
  • 9
2

You can try this...

private void shareTextUrl() {
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("text/plain");
        share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

        share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
        share.putExtra(Intent.EXTRA_TEXT, "<source url>");

        startActivity(Intent.createChooser(share, "Share text to..."));
    }
Kshitij Jhangra
  • 577
  • 7
  • 14
0

Here is my implementation, you can add more apps if you wish by just adding package names. This code also sorts the applications according to their names.

    List<Intent> targetShareIntents = new ArrayList<Intent>();
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    PackageManager pm = getActivity().getPackageManager();
    List<ResolveInfo> resInfos = pm.queryIntentActivities(shareIntent, 0);
    if (!resInfos.isEmpty()) {
        System.out.println("Have package");
        for (ResolveInfo resInfo : resInfos) {
            String packageName = resInfo.activityInfo.packageName;
            Log.i("Package Name", packageName);

            if (packageName.contains("com.twitter.android") || packageName.contains("com.facebook.katana")
                    || packageName.contains("com.whatsapp") || packageName.contains("com.google.android.apps.plus")
                    || packageName.contains("com.google.android.talk") || packageName.contains("com.slack")
                    || packageName.contains("com.google.android.gm") || packageName.contains("com.facebook.orca")
                    || packageName.contains("com.yahoo.mobile") || packageName.contains("com.skype.raider")
                    || packageName.contains("com.android.mms")|| packageName.contains("com.linkedin.android")
                    || packageName.contains("com.google.android.apps.messaging")) {
                Intent intent = new Intent();

                intent.setComponent(new ComponentName(packageName, resInfo.activityInfo.name));
                intent.putExtra("AppName", resInfo.loadLabel(pm).toString());
                intent.setAction(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, "https://website.com/");
                intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_text));
                intent.setPackage(packageName);
                targetShareIntents.add(intent);
            }
        }
        if (!targetShareIntents.isEmpty()) {
            Collections.sort(targetShareIntents, new Comparator<Intent>() {
                @Override
                public int compare(Intent o1, Intent o2) {
                    return o1.getStringExtra("AppName").compareTo(o2.getStringExtra("AppName"));
                }
            });
            Intent chooserIntent = Intent.createChooser(targetShareIntents.remove(0), "Select app to share");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetShareIntents.toArray(new Parcelable[]{}));
            startActivity(chooserIntent);
        } else {
            Toast.makeText(getActivity(), "No app to share.", Toast.LENGTH_LONG).show();
        }
    }
Oguz Ozcan
  • 1,694
  • 19
  • 26
-4

For me this works just fine:

startActivity(Intent.createChooser(new Intent(Intent.ACTION_SEND,
    Uri.parse("http://...")),"Share URL"));// share url is on your own
Milad Yarmohammadi
  • 1,253
  • 2
  • 22
  • 37
Bacardi
  • 13
  • 1
  • 1
  • 4
  • 1
    Not working : error "no apps can perform this action" – stackflow May 27 '15 at 09:51
  • 1
    reason it give no apps can perform this action is that no mimetype has been specified. App filter SEND action on type. If you do not specify it (text/plain) no app will intercept that intent – Daniele Segato Jul 22 '15 at 10:02