Create Custom Twitter Follow Button in Android Application

Hello friends ,
Today i am going to post my second tutorial.

In this tutorial I will explain how to create the custom "Twitter Follow" button in our android application.

With this button we can the follow the particular person on twitter.
For this you have to first integrate Fabric API provided by Twitter. Here is a link to integrate this api -  https://get.fabric.io/
There are several simple to steps integrate this api and you can use Twitter Login feature in just few steps by using it.
Now we see how to create Follow Functionality in our application.
First you have to create a simple "Follow Me"button in layout of the activity on which click event we have to implement Follow functionality.
(Note - I am assuming user is already logged in twitter  using Fabric API).

I am creating a class "TwitterFollowMe" and a interface to hit twitter api of following a person as -

public class TwitterFollowMe extends TwitterApiClient {
    public TwitterFollowMe(TwitterSession session) {
        super(session);
    }
    public FollowService getFollowService() {
        return getService(FollowService.class);
    }
    /*interface used for Api call for Following*/
    public interface FollowService {
        @POST("/1.1/friendships/create.json")
        public void create(@Query("screen_name") String screen_name,
        @Query("user_id") String user_id, @Query("friends") boolean follow, 
        Callback<User> cb);
    }
}

Now on "Follow Me" button click event you have to implement that interface as -

btnFollowMe.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        apiClient = new TwitterFollowMe(session);
        //twitter user name
        apiClient.getFollowService().create("Twitter_Screen_Name(ex-Moto_IND)", 
        null, true, new Callback<User>() {
            @Override
            public void success(Result<User> result) {
                Toast.makeText(MainActivity.this, "Thanks for following!", 
                Toast.LENGTH_SHORT).show();
                btnFollowMe.setVisibility(View.GONE);
            }

            @Override
            public void failure(TwitterException e) {
                Toast.makeText(MainActivity.this, "Error following", 
                Toast.LENGTH_SHORT).show();
            }
        });
    }
});

In this code we call "getFollowService().create" method .
First parameter is the "Twitter Screen Name" of the user and the second parameter is "Twitter User Id" and the third parameter is boolean to indicate Follow action and the last is the Callback.
You can provide any one parameter from the  "User Id" and "Screen Name".
In "success" callback you can get the call after successfully following that "Twitter User".

And that's all to implement the Twitter Follow button!!!!.

Thanks

1 comment:

  1. Your post has those facts which are not accessible from anywhere else. It’s my humble request to u please keep writing such remarkable articles Android

    ReplyDelete

Flutter : Using Form and Text Fields

Flutter is a popular cross-platform mobile development framework that allows developers to build native apps for both Android and iOS using ...