How to sent a tweet with Fabric

on

 

Fabric  is the official Android SDK from Twitter. It’ s a very powerful tool that offers a lot of functionalities, plus the documentation is extremely good ( I’m looking at you Facebook ). Composing a Tweet from an app could be very useful, so let’s go down the steps you need to do.

 

Supposing you have Fabric imported, the first thing you need to do is add the twitter dependency in your build.gradle file:

 

[java]

compile(‘com.twitter.sdk.android:tweet-composer:1.0.3@aar’)

{ transitive = true; }

 

[/java]

 

Then, you need add this code to the oncreate method of your Activity.

[java]

TwitterAuthConfig authConfig = new TwitterAuthConfig(“consumerKey”, “consumerSecret”);

Fabric.with(this, new TwitterCore(authConfig), new TweetComposer());

[/java]

 

Lastly you are “building” the tweet by calling the TweetComposer.Builder.

[java]

TweetComposer.Builder builder = new TweetComposer.Builder(this)

.text(“This is your tweet! Write whatever you want !”);

builder.show();

[/java]

 

Now let’s say that you have a button with the onClick set equal to a method, let’s say composetweet. Your code should look like this:

[java]

public void composetweet( View view) {

TweetComposer.Builder builder = new TweetComposer.Builder(this)

.text(“This is your tweet! Write whatever you want !”);

builder.show();

}

[/java]

 

You can add a lot more to a tweet than just a string. Check out the official documentation for composing a tweet from Twitter’s site.

If this guide helped you, comment below.

 

Leave a Reply

Your email address will not be published. Required fields are marked *