-1

How can I share a text to a social medial site like facebook and twitter by clicking an imagebutton? I've seen one question here that also asks this kind of question but then I can't understand. This is my first app so I am hoping anyone here can help me with this. Thank you in advance.

P.S. Please don't be so rude answering questions. :)

Lab Ley
  • 151
  • 1
  • 12
  • Take a look [link](http://stackoverflow.com/a/24608685/4385913) and [link](http://stackoverflow.com/questions/3515198/share-text-on-facebook-from-android-app-via-action-send) – Skizo-ozᴉʞS ツ Jan 30 '15 at 10:56

2 Answers2

6

Try this:

for facebook

Intent intent = new Intent(Intent.ACTION_SEND);
intent .setType("text/plain");
intent .setPackage("com.facebook.katana");
intent .putExtra(Intent.EXTRA_TEXT, "Hello World");
try {
    activity.startActivity(intent );
} catch (android.content.ActivityNotFoundException ex) {
    ToastHelper.MakeShortText("Facebook have not been installed.");
}

for twitter

Just change the package name to com.android.twitter

if you have facebook and twitter install on your device then it will direct share your text, else will show a toast of error.

Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
2

If you want more than just Facebook and Twitter you can use the send intent.

So for the on click listener for the image button you could use this:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Text to share.");
shareIntent.setType("text/plain");
startActivity(shareIntent);

The benefit of this is that you can offer the user the ability to share on more than just Facebook and Twitter (any app they have installed that supports sharing of types "text/plain") and you don't have to check to see (or handle) certain apps being installed.

Vahid Haratian
  • 708
  • 2
  • 6
  • 23
Sam Bains
  • 548
  • 2
  • 8