Sharing URL to Facebook, Twitter and email in Android?

Solution 1:

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:

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/android/support/v4/app/ShareCompat.html

Solution 3:

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:

// 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:

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