Check out the Latest Articles:

I’m currently making an application with some persons for the next FOSDEM conference here in Brussels. One of the things I wanted to add was a way to tweet/facebook/text/email what conference you were attending. I looked it up and found that I could use the intent action ACTION_SEND together with an IntentChooser. This way the user can choose whatever application that can receive the ACTION_SEND intent: email, Twitter, Facebook, Text,…

device

For sharing, I used a function that takes as a parameter the subject and text. You could add the context to the parameters and make the method static in a utility class, for example.

Here are the few lines of code to use:

public void share(String subject,String text) {
 final Intent intent = new Intent(Intent.ACTION_SEND);

 intent.setType("text/plain");
 intent.putExtra(Intent.EXTRA_SUBJECT, subject);
 intent.putExtra(Intent.EXTRA_TEXT, text);

 startActivity(Intent.createChooser(intent, getString(R.string.share)));
}

I know there are a lot of sites out there that are telling the exact same thing, but I’m using my own blog to keep my own code snippets and share them, so enjoy.



  1. Pascal on Saturday 23, 2010

    Yesterday I said “hmm, I have to search how to program a ’share’ in Android”. Today, I have the answer without searching … Cool ;-)

  2. Janusz Leidgens on Saturday 23, 2010

    Hello I use this exact piece of code but I only get mail applications in the chooser. Twidroid and textmessage etc are missing.

    Do you happen to know why?

  3. tossking on Saturday 23, 2010

    I was troubled by this problem for several days, thanks for this code.

  4. CrazeAndroider on Saturday 23, 2010

    It doesn’t work for facebook here is my code, can you help me

    public void share(String subject, String text) {
    final Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType(”text/plain”);
    intent.putExtra(Intent.EXTRA_SUBJECT, “Sharing”);
    intent.putExtra(Intent.EXTRA_TEXT,”Check out site \”Google\” @ http://google.com“);

    startActivity(Intent.createChooser(intent,”Select an action for sharing”));
    }

  5. Lakshmanan on Saturday 23, 2010

    @ Janusz Leidgens,
    @ CrazeAndroider

    This code will call only programs which are installed in your device, if u installed facebook or twitter that also will called by the indent, so that it will displayed in the list. If you check with emulator the compose mail will directly open.