Sharing URL to Facebook, Twitter and email in Android?

AndroidFacebookEmailTwitterSharing

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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!

Solution 2 - Android

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

Solution 3 - Android

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

Solution 4 - Android

// 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!"));

Solution 5 - Android

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..."));
    }

Solution 6 - Android

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();
        }
    }

Solution 7 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsunilView Question on Stackoverflow
Solution 1 - AndroidLior IluzView Answer on Stackoverflow
Solution 2 - AndroidlukjarView Answer on Stackoverflow
Solution 3 - AndroidMuhannad A.AlhaririView Answer on Stackoverflow
Solution 4 - AndroidAshish ChauguleView Answer on Stackoverflow
Solution 5 - AndroidKshitij JhangraView Answer on Stackoverflow
Solution 6 - AndroidOguz OzcanView Answer on Stackoverflow
Solution 7 - AndroidBacardiView Answer on Stackoverflow