Fabric is an amalgamation of multiple tools and SDKs that help developers create awesome apps in no time. Using their plugin for Android Studio, we can integrate any or all of their SDKs in our apps, Twitter is included.
In this tutorial, we will see how to implement Twitter API integration in our Android app. Let’s start with App Module‘s build.gradle file:
Add these statements at the top of the file:
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
And in Dependencies section, add this:
compile('com.twitter.sdk.android:twitter:2.3.1@aar') {
transitive = true;
}
In your activity’s xml file, add the TwitterLoginButton like this:
<com.twitter.sdk.android.core.identity.TwitterLoginButton
android:id="@+id/btnTwitter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
Finally, add this code in your Activity file:
// Note: Your consumer key and secret should be obfuscated in your source code before shipping.
private static final String TWITTER_KEY = "YOUR_TWITTER_API_KEY";
private static final String TWITTER_SECRET = "YOUR_TWITTER_API_SECRET";
private TwitterSession session = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter);
ButterKnife.bind(this);
setTitle("Twitter");
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig), new TweetComposer());
btnTwitter.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
// The TwitterSession is also available through:
// Twitter.getInstance().core.getSessionManager().getActiveSession()
session = result.data;
// TODO: Remove toast and use the TwitterSession's userID
// with your app's user model
String msg = "@" + session.getUserName()
+ " \n logged in! (#" + session.getUserId() + ")";
tvTwitterUser.setText(msg);
// Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
@Override
public void failure(TwitterException exception) {
Log.d("TwitterKit", "Login with Twitter failure", exception);
}
});
}
@OnClick({R.id.btnTweet})
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnTweet:
if (etTweet.getText().toString()!=null && etTweet.getText().toString().length()>0) {
TweetComposer.Builder builder = new TweetComposer.Builder(this)
.text(etTweet.getText().toString());
builder.show();
}
else {
UIUtils.showToast(this, "Please enter your tweet.");
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Make sure that the loginButton hears the result from any
// Activity that it triggered.
btnTwitter.onActivityResult(requestCode, resultCode, data);
}
You can check the full source here.
Happy Coding!