Recently, I had to implement Twitter sharing in my new app that I have been working on recently. Upon searching about how to do this, I found out a library called Twitter4J. This library has some decent functions but it’s not that simple to implement as compared to the Facebook SDK which handles all the stuff within and exposes some handlers for post-authentication tasks.
Anyhow, I continued my research about using this library and found out some implementations that launch the browser for authentication and then invoking your application back when the process is complete. From there onwards, things become simple.
I however didn’t like the idea and rolled over my sleeves to code what I had in my mind. Instead of loading the browser, all I did was put a webview in my activity to load the authentication url in it. Upon completing authentication, it is redirected to the callback url that is specified while initializing the twitter object. Checkout the code below:
You can specify any custom URL in place of CALLBACKURL (which is defined as a constant in my class). After this, all you have to do is to place a check in your WebViewClient’s onPageStarted method to find out if the authentication is done and the twitter has redirect you back to your callback URL.
Almost done! Now you can use the updateStatus function to update the status on Twitter.