15

I am using something like this to share some text using available applications on the user's phone.

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

My main problem is that I would like to have a different text if the user chooses Twitter instead of email for example (short version with short URLs VS full text with attached images).

How can ont find out which application the user has decided to use?

1

3 Answers 3

8

Once you hand the text off to the system with createChooser its out of your hands, no way to change the text after that.

4
  • So I'd have to build my own chooser? :-/ Clumsy API design isn't it? In that case, any sample code on how to create the intent specifically for Twitter or Facebook for instance? Dec 13, 2010 at 17:11
  • 2
    the app in question would have to have published its own public api for sending tweets, update status, etc. Twitdroid is the only app I know of that has done so and no others follow its convention. I personally don't mind this design I think the convenience is worth the trade-off. Dec 13, 2010 at 17:13
  • It is indeed convenient, however it is rather hard to adapt to constraints on message length for instance. I might just add a dialog that asks the user if he want to share the thing as a short or a long text. Not optimal as far as the number of clicks to achieve what you need but only way I can think of so far. Thanks for the input. Dec 14, 2010 at 12:40
  • 3
    You can specify the package for an intent, but I would advice against that. Instead make your own chooser which has a few special cases for some services (Twitter). Even better would be to make your own chooser with a checkbox for "Shorter text" or something. Jul 28, 2011 at 8:41
0
 Intent intent = new Intent(Intent.ACTION_SEND);

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

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

On click Social(ex-twitter)

shareOnSocial(activity, h.shareTwitter, Global.TWITTER_ID, shareContent, activity.getResources().getString(R.string.error_twitter));

On Create Chooser

shareOnSocial(activity, h.shareChooser, "choose", shareContent, activity.getResources().getString(R.string.error));

Method share

private void shareOnSocial(Activity mAct, View shareView, String packageId, String content, String error) {
    shareView.startAnimation(clickAnimation);
    Intent i = new Intent();
    i.setAction(Intent.ACTION_SEND);
    i.putExtra(Intent.EXTRA_TEXT, content);
    i.setType("text/plain");

    if (!packageId.equals("choose")) {
        i.setPackage(packageId);
        try {
            mAct.startActivity(i);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(mAct, error, Toast.LENGTH_SHORT).show();
        }
    } else {
        mAct.startActivity(Intent.createChooser(i, mAct.getString(R.string.share)));
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.