Integrate Social Media with Android app: Part 2/3

Dishant gandhi
3 min readJun 30, 2020

Welcome to the part 2 of 3 where we will be integrating social media with android app

Photo by Yucel Moran on Unsplash

Part 2: Twitter Login

Here, We are adding twitter login and showing their twitter username inside the TextView.

First, Let’s become Twitter Developers click here to go to website for getting started with twitter developers.

Sign up for Twitter API it will send you verify mail on your registered mail id so verify it can get started with API

Now, go to the dashboard and follow the steps given below:

After you click on apps, you will be able to see a screen without any apps

So, click on create app, Enter all the information and when you reach Callback Url enter the following text:

twittersdk://

After following all the steps your screen will look like this:

fill all the details and submit it. You will then able to access 2 keys

  • API key
  • API secret key

these 2 keys we are going to add in strings.xml so that we can access it later in app.

Before we add Button let’s add dependency for twitter core

implementation 'com.twitter.sdk.android:twitter-core:3.1.1'

Now, let’s add Button and TextView..

TextView will be same as our Facebook’s TextView so below is the code:

<TextView
android:id="@+id/twitter_user_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_margin="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/facebook_login" />

Now, adding Button in activity xml

<com.twitter.sdk.android.core.identity.TwitterLoginButton
android:id="@+id/twitter_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/twitter_user_id"/>

Let’s start with setting up the button so that user can login successfully with twitter:

  • Step 1: add twitter config before inflating layout/setting content view
TwitterConfig config = new TwitterConfig.Builder(this)
.logger(new DefaultLogger(Log.DEBUG))
.twitterAuthConfig(new TwitterAuthConfig(getResources().getString(R.string.twitter_api_key), getResources().getString(R.string.twitter_api_secret_key)))
.debug(true)
.build();
Twitter.initialize(config);
  • Step 2: adding callback for login into twitter and getting User name and adding it inside text view
twitterLogin.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
Toast.makeText(MainActivity.this, "Sucess", Toast.LENGTH_SHORT).show();
TwitterSession session = TwitterCore.getInstance().getSessionManager().getActiveSession();
String name = session.getUserName();
twitterUserId.setText(name);
}

@Override
public void failure(TwitterException exception) {
Log.e("Twitter Login",""+exception.getMessage());
Toast.makeText(MainActivity.this, "failed", Toast.LENGTH_SHORT).show();
}
});
  • Step 3: adding button inside onActivityResult()
twitterLogin.onActivityResult(requestCode, resultCode, data);
  • Step 4: adding logout for twitter on app gets closed i.e Activity gets Destroyed
@Override
protected void onDestroy() {
super.onDestroy();
TwitterCore.getInstance().getSessionManager().clearActiveSession();
}

You have successfully added Twitter login and showing their User name inside the TextView

Bonus Time!!

I have added view Binding inside this project using ButterKnife

So, how to use it??

Add dependency inside app gradle

implementation 'com.jakewharton:butterknife:10.2.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'

also add this compile Options inside android{…}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

Now, Let’s Bind the Views so that we can extend their scope

@BindView(R.id.facebook_login)
LoginButton facebookLogin;

Now, bind the view inside the onCreate()

ButterKnife.bind(this);

This will Bind Views and we can access it inside our class

Note: Twitter will be asking to authorise app after clicking on Login Button. So, press authorise app instead of cancel to get success.

Your app will look like this:

The GitHub repository has a separate branch for this new update as Twitter

You can connect with me on LinkedIn and Instagram

Don’t forget to clap for yourself because it is hard to integrate Twitter API with android

If you missed previous part here is the link -> Facebook

Next Part will be Easiest and Amazing for you all!!!

--

--