Nowadays you must have seen while longing in many apps provides the login option directly using Facebook, Twitter, Linkedin and Google account, this process saves the time of filling a long registration form and also makes the user to login easily anytime. So, in this tutorial I had have explained about How You Can Integrating Twitter in Android.
![](http://findnerd.s3.amazonaws.com/imagedata/8494/8494.jpeg)
If you are willing to integrate Twitter login for your app then please go through the simple steps mentioned below and enjoy the power of code:
Step 1) Add the dependencies to your project to enable twitter sdk
you need to add the below line
compile 'com.twitter.sdk.android:twitter:3.1.1'
in your app's build.gradel(Module:app), after adding this sync your app.
Step 2) Now go to twitter app dashboard and create a new app below is the link for the same, this will be generating CONSUMER_KEY and CONSUMER_SECRET that will be used within your app for authentication
https://apps.twitter.com/
Step 3) Now add CONSUMER_KEY and CONSUMER_SECRET in your string.xml file as shown below
<string name="com.twitter.sdk.android.CONSUMER_KEY"> 7Ua1fRWsqpecLDCSUsyp3JkpG</string>
<string name="com.twitter.sdk.android.CONSUMER_SECRET"> ue7JIHeVVtk56gX3yDXGMpWAqpxGSJjfpTibOyp6kyhA1LNYLf</string>
Step 4) Now initialize Twitter kit by using the line
Twitter.initialize(this);
in your Activities onCreate method.
Step 5) Now add a Twitter login button in your activities layout (xml) file as shown here
<com.twitter.sdk.android.core.identity.TwitterLoginButton
android:id="@+id/login_button"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Step 6) Now add the code given below in your corresponding activity to handle the functioning of twitter login Button
public class LoginActivity extends AppCompatActivity {
TwitterLoginButton loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Twitter.initialize(this);
loginButton = (TwitterLoginButton) findViewById(R.id.login_button);
loginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
Toast.makeText(LoginActivity.this, "Logged in successFully", Toast.LENGTH_SHORT).show();
// Do something with result, which provides a TwitterSession for making API calls
}
@Override
public void failure(TwitterException exception) {
// Do something on failure
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Pass the activity result to the login button.
loginButton.onActivityResult(requestCode, resultCode, data);
}
}
That's all, You have integrated Twitter to your app. If you have any questions or feedback, please feel free to write in comments below.
0 Comment(s)