Categories: Android

Twitter Integration in Android Using Fabric

Hello guys!

Today I am going to show you how to integrate Twitter in Android using Fabric SDK.

First of all go to Fabric and Sign Up for an account if you haven’t already have one.

The open your project. I already have set up a simple layout and written their references to the buttons and Listeners in MainActivity.

After that being done. Click on your project Settings, then plugin.

Then Click Browse repositories. After that type in Fabric and Install Fabric For Android Studio.

After Android Studio reopens. In top of your bar click the Fabric Icon. It will open a window on right side. Click on the power button. It will ask you for your Fabric login credentials. Enter them and log in.

When login completes it will create a project and will show your project package name. Select it,  then Next.

On new screen it will list all the Kits provided by Fabric, select Twitter then on next page select Install.

It will ask you to create twitter account, (if you already have one you’ll need to add its credentials). For now click Create Account.

Next, it will ask you to show you the changes it will apply to your project. Click Apply.

Create two variables, once gradle finishes syncing. Don’t forget to add onActivityResult in your class.

    TwitterAuthClient twitterAuthClient;
    TwitterSession twitterSession;
   
   //Instantiate twitterSession after setContentView; like below
    TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
    Fabric.with(this, new Twitter(authConfig));
    setContentView(R.layout.activity_main);
    twitterAuthClient = new TwitterAuthClient(); 

   //Also add onActivityResult
    @Override
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        super.onActivityResult(requestCode,responseCode,intent);
        twitterAuthClient.onActivityResult(requestCode, responseCode, intent);
    }

Then in your Login Button listener. call the following method.

   private void twitterLogin(){
        twitterAuthClient.authorize(this, new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {
                twitterSession = result.data;
                Twitter.getApiClient(twitterSession).getAccountService().verifyCredentials(true, false, new Callback<User>() {
                    @Override

                    public void success(Result<User> userResult) {
                        User currentUser = userResult.data;
                        String name =  currentUser.name;
                        String userName = currentUser.screenName;
                        String profilePicture = currentUser.profileImageUrl;
                        TwitterSession twiiterSession = Twitter.getInstance().core.getSessionManager().getActiveSession();
                        String userId = String.valueOf(twiiterSession.getUserId());
                        Log.d("name",name);
                        Log.d("userName",userName);
                        Log.d("profilePicture",profilePicture);
                        Log.d("userId",userId);
                        login.setVisibility(View.GONE);
                        logout.setVisibility(View.VISIBLE);
                        Toast.makeText(getApplicationContext(),name+"\n"+userName+"\n"+profilePicture+"\n"+userId,Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void failure(TwitterException e) {
                    }
                });



            }

            @Override
            public void failure(TwitterException exception) {

            }
        });



    }

Lastly, in your Logout Button listener, do the following.

           
            if (twitterSession != null) {
                    
                    Twitter.getSessionManager().clearActiveSession();
                    twitterSession = null;
                    Twitter.logOut();
                    Log.d("Logged Out!","Logged Out!");
                    login.setVisibility(View.VISIBLE);
                    logout.setVisibility(View.GONE);
                    Toast.makeText(getApplicationContext(),"Logged Out", Toast.LENGTH_SHORT).show();
                }

Run your project. Click twitter icon. Then Allow.

You’ll get your username, name, profile picture link and user id as soon as you’re logged in.

NOTE:
Your Image url will be something like “734074822397943809/nWcfwCDj_normal.jpg”. If you want the original picture, you’ll need to remove the substring _normal and make it like “734074822397943809/nWcfwCDj.jpg” to get the original size picture.

That’s all for the day.
If you have any question, leave a comment.
Link to project on Github.
See you all next time. 🙂

Aaqib Hussain

Aaqib is an enthusiastic programmer with the love of Swift and anything that looks like Swift i.e Kotlin. He loves writing code in Swift, and exploring new technology and platforms. He likes to listen to old music. When he is not writing code, he's probably spend his time watching movies, tv-shows or anime, or either doing some research for writing the next article. He started Kode Snippets in 2015.

Recent Posts

Things to know when moving to Germany

This article covers some important things you must know when you are considering a move…

3 years ago

Unit Testing in Android for Dummies

What is Unit Testing? In its simplest term, unit testing is testing a small piece…

4 years ago

Factory Design Pattern

In this article, you will learn about a type of Creational Design Pattern which is…

5 years ago

Creating Target specific Theme in iOS

In this tutorial, you will go through the use of targets to achieve two separate…

5 years ago

Facade Design Pattern

In this article, you will learn about a type of Structural Design Pattern which is…

5 years ago

Singleton Design Pattern

In this article you will learn about a type of Creational Design Pattern which is…

5 years ago