Compose Tweets

The TweetComposer Kit provides two ways to compose Tweets:

  1. Launch the Twitter Application’s Tweet Composer - a feature-rich composer which supports attaching images and videos.
  2. Launch the Twitter Kit App Card Composer - a lightweight composer which lets users compose Tweets with App Cards from within your application.

Set Up Kit

Note

Skip this section if you installed Twitter Kit using the Twitter Kit installation instructions.

Add the TweetComposer Kit to your build.gradle dependencies. View the TweetComposer changelog for the latest version number.

dependencies {
    compile('com.twitter.sdk.android:tweet-composer:2.3.1@aar') {
        transitive = true;
    }
}

and be sure the TweetComposer Kit is included in your application’s Fabric.with() call.

import io.fabric.sdk.android.Fabric;
import com.twitter.sdk.android.tweetcomposer.TweetComposer;
...

@Override
public void onCreate() {
    super.onCreate();

    TwitterAuthConfig authConfig =  new TwitterAuthConfig("consumerKey", "consumerSecret");
    Fabric.with(this, new TwitterCore(authConfig), new TweetComposer());
}

Compose a Tweet

The Twitter Android Application allows apps to start the exported Tweet composer via an Intent. The Twitter Android compose is feature-rich, familiar to users, and has options for attaching images and videos.

Build an Intent

After initializing the TweetComposer Kit with Fabric, start construction of a Tweet composer by using the Builder.

import com.twitter.sdk.android.tweetcomposer.TweetComposer;
...

TweetComposer.Builder builder = new TweetComposer.Builder(this)
     .text("just setting up my Fabric.")
     .image(myImageUri);
builder.show();

The image Uri should be a file Uri (i.e. file://absolute_path scheme) to a local file. For example,

File myImageFile = new File("/path/to/image");
Uri myImageUri = Uri.fromFile(myImageFile);
../_images/compose-tweet.png

Note

If the Twitter app is not installed, the intent will launch twitter.com in a browser, but the specified image will be ignored.

Twitter Kit App Card Composer

The Twitter Kit App Card Composer is a lightweight composer which lets users compose Tweets with App Cards from within your application. It does not depend on the Twitter for Android app being installed.

../_images/tweet-composer-with-app-card.png

Intent Builder

After authenticating a user, build an Intent to start the TweetComposer ComposerActivity and include the TwitterSession.

final TwitterSession session = TwitterCore.getInstance().getSessionManager()
    .getActiveSession();
final Intent intent = new ComposerActivity.Builder(YourActivity.this)
    .session(session)
    .createIntent();
startActivity(intent);
Builder Method Description
session(TwitterSession) Set the TwitterSession of the User to Tweet
card(Card) Attach an App Card to the Tweet
hashtags(hashtags...) Hashtags to prefill in composer
darkTheme() Use the dark composer theme, defaults to light

App Cards

If you’d like to attach an Application Card to the Tweet, create a Card with the AppCardBuilder.

A live Google Play Store Android package name is required for an App Card to be rendered in the Twitter App. The Builder automatically detects your application’s package name (e.g. “com.twitter.android”) from the context argument.

final Card card = new Card.AppCardBuilder(YourActivity.this)
  .imageUri(uri)
  .iPhoneId("123456")
  .iPadId("123456")
  .build();
Builder Method Description
imageUri(uri) Sets the App Card image Uri
iPhoneId(id) Set the iPhone app Id from the Apple App Store
iPadId(id) Set the iPad app Id from the Apple App Store
googlePlayId(package) Override the detected app package name

Include the card in the Intent to ComposerActivity.

final Intent intent = new ComposerActivity.Builder(YourActivity.this)
    .session(session)
    .card(card)
    .hashtags("#fabric", "#twitter")
    .createIntent();
startActivity(intent);

Requirements

Note

Your application must first be whitelisted by Twitter in order to post a Tweet with an App Card. Please review App Card Composer Usage Guidelines for requesting access.

App Cards have several requirements:

  • Specify a Card image Uri to a local media file on the device.
  • Specify the package name of a live Google Play application.
  • The Card image must be at least 800 pixels wide and 420 pixels tall, otherwise the Card won’t be rendered in the Twitter Android and iOS apps.
  • We recommend an aspect ratio of 1.91:1 to provide the best rendering. Other aspect ratios will be cropped.
  • Card must comply with App Card Composer Usage Guidelines.

Results

After attempting to post a Tweet, the ComposerActivity TweetUploadService broadcasts an Intent with the action value com.twitter.sdk.android.tweetcomposer.UPLOAD_SUCCESS for success or com.twitter.sdk.android.tweetcomposer.UPLOAD_FAILURE for failure. On success, the Intent will contain an extra value with the Tweet ID of the created Tweet. On failure, the Intent will contain a copy of the original intent which could be used to retry the upload.

You can create a BroadcastReceiver to receive these Intents.

public class MyResultReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    if (TweetUploadService.UPLOAD_SUCCESS.equals(intent.getAction())) {
      // success
      final Long tweetId = intentExtras.getLong(TweetUploadService.EXTRA_TWEET_ID);
    } else {
      // failure
      final Intent retryIntent = intentExtras.getParcelable(TweetUploadService.EXTRA_RETRY_INTENT);
    }
  }
}

Don’t forget to add your BroadcastReceiver to the application manifest.

<receiver
  android:name=".MyResultReceiver"
  android:exported="false">
  <intent-filter>
    <action android:name="com.twitter.sdk.android.tweetcomposer.UPLOAD_SUCCESS"/>
    <action android:name="com.twitter.sdk.android.tweetcomposer.UPLOAD_FAILURE"/>
  </intent-filter>
</receiver>