Learning Android: Sharing with Twitter/the 'share via' dialog
Date: 2011-12-29 22:40:19

One thing I wanted to do in the little application I'm working on was send data to other apps on my phone using the 'share via' dialog which I've seen used on the https://market.android.com/details?id=com.twitter.android&hl=en[Twitter app].

In this case I wanted to send a link and its title to twitter and came across http://stackoverflow.com/questions/3553017/android-share-via-dialog[a StackOverflow post which explained how to do so].

To keep it simple I added a button to the view and then shared the data via the on click event on that button:

[source,java]
----

Button button = createButton();
button.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Stupid Scanner tricks - http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html");
    v.getContext().startActivity(Intent.createChooser(shareIntent, "Share via"));
  }
});
----

image::{{<siteurl>}}/uploads/2011/12/android-share-via.jpg[Android share via,226]

In this case if I choose the Twitter app from the drop down list that appears it will create a new tweet with the value of 'android.content.Intent.EXTRA_TEXT' as its body.

If we needed to pass a subject to the other app then we could set that using 'android.content.Intent.EXTRA_SUBJECT' but in this case it's unnecessary.

From what I understand so far only apps which can handle the 'text/plain' format will show up in the drop down list but that seems to be pretty much every app on my phone.

This works reasonably well but I wanted to see if it was possible to share directly with the Twitter app rather than having to choose from a selection of apps.

Via a http://lisudza.wordpress.com/2010/12/29/tweeting-using-twitter-for-android-intent/[combination] http://blog.rafaelsanches.com/2010/06/18/using-native-twitter-app-intent-to-share-on-android/[of blog posts] and http://stackoverflow.com/questions/7545254/android-and-facebook-share-intent[StackOverflow questions] I came across the following solution for posting directly to the Twitter app:

[source,java]
----

Button button = createButton();
button.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Some text");
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Stupid Scanner tricks - http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html");

    final PackageManager pm = v.getContext().getPackageManager();
    final List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
    for (final ResolveInfo app : activityList) {
      if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) {
        final ActivityInfo activity = app.activityInfo;
        final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
        shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        shareIntent.setComponent(name);
        v.getContext().startActivity(shareIntent);
        break;
      }
    }
  }
}):;
----

It does depend on the Twitter app being installed on the phone to work but since the app is just for me I think it's ok for the moment.