Thursday 25 October 2018

Twitter login button Android.

There is no proper tutorial on twitter-kit-android Github.
After some search on google I finally found the way.
If you encountered an error, check at the end of the post for some solutions

Create app in developer console: developer.twitter.com/apps

Make sure you selected Sign in with Twitter.

To make android app to work, you need to add the scheme: 
twittersdk://
as callback url

Import the library :

If you just need login with twitter button, just use core
otherwise include entire libraby

Adding the TwitterLoginButton in layout:


The rendering error:

If you are targeting SDK version 28, you may get error in the design file.
The error might be 

The error is because of corners of twitter button background.
So you have to manually fix the error by creating a manual class that extend TwitterLoginButton.
If you want you can use the files in the below link.


Setup the button:

Add the following code in onCreate(Bundle savedInstanceState) method of your activity.  If you are adding the button in fragment, then add the code in the enclosing activity. I mean in the activity the fragment/fragment adapter is getting used.
This code needs to be before setContentView(int/view) method call.

Initialise the button and set callback in activity/fragment:


Send the activity result to TwitterLoginButton

In onActivityResult method of activity or fragment, 
if you are using the button inside fragment, then send the activity result back to fragment,

Possible errors while running or at runtime:

If you get an error saying some thing like this,
add the following in your module level build.gradle file scope android ( means inside android{})
If you get message saying "Failed to get request token", make sure you added twittersdk:// in callback urls or schemes.
If you get message like "Authorise token error", make sure you copied the consumer kay and secret correctly.