Many people use Twitter4J for coding Twitter clients for Android, but I couldn't find a single example of how to implement "Sign-in with twitter" in my Android applications, as Facebook Single Sign-on works.
So I coded my own.

I took references from sign-post methodology and Twitter4J itself, merged goods of both and finally had what I wanted.

Once the user allows/denies permissions to your app, the browser redirects to the callback URL, which you had provided in the second step, and if you're going by my code, that callback URL calls your activity back.

When you're activity gains the focus again, you separate the token and secret from it.

You then create an AccessToken with those token and secret, and use this AccessToken for interaction with twitter.

You can (you should) also save the token and secret and use it to create AccessToken for future interactions with twitter.

If the user wants to logout, simply delete the token and secret from your app. Since twitter4j works in complete stateless manner, you don't have to do anything more.

Once logged in, you'll have access to user's twitter account untill one of the two things happen: 1) The user logs out, or 2) The user revokes access to your app from twitter.

Jyoti, that's completely normal.If you have noticed, on the web too, if any application wants to access your twitter account, it opens up a new browser window, opens twitter in it, and uses it to authenticate.

The idea is, The user provides his/her credentials only to the service you have created a client of. This increases the level of trust users are going to put on your application, since they know that you aren't storing their username/passwords, but are asking twitter for it.

Actually i gone through one example of android twitter in which sugree has created apk for twitter like as we have in facebook i.e https://github.com/sugree/twitter-android-sdk. It is very simple and also opening on our application area onli, but in his api file there is no method define for posting tweet.

AA aman,Its pretty much clear now after your great help and time. just was wondering if we can have a button for Login/logout, so that i can login/logout any time and can login with diff id... have no issue in login but i searched the twitter4j and stackoverflow but didn't find anything regarding logout. As you were saying to delete the accesstoken on logout, could you explain this more and through code example.

Hey Aman what would be the CALLBACK_URL as when i try to make it Browser instead of Client, it is asking URL and when i provided, the one given in ur sample code, twitter is not accepting saying u mail us the call back url and we would get back 2 u, they mailed me saying we are working on it but no updates then after.

Hey Aman thanks for your tutorial, it helped me a lot, and still problem persist with inbuilt browser of my device with 2.1update1 but somehow i managed to get through this with opening twitter authUrl in WebView and got callback from Twitter.

@Ankit This was going to be my next step, to include a custom webview which handles the redirect. Kudos to you for that, although I have done that only in my real world implementations, but couldn't post it here.

@Farhan Buddy, perhaps I have told you before too, that twitter4J is stateless. you can have a button which does login/logout.Follow all the OAuth stuff when the login button is clicked, and when logout is clicked, delete the AccessToken that you saved.

@Jyoti I tried to dig into sugree's example, but it didn't work for me. Do you have any screenshot?

nice tutorial. It is working fine on emulator but when i try to run my application on real device, web browser doesn't open. I get this exception."Authentication credentials were missing or incorrect. Failed to validate oauth signature and token." Same code is working fine on emulator. Can anyone help me out please?

Hi, thanks for the guide, but i've an error when call getOAuthRequestToken, he says "The request is understood, but it has been refused. SSL is required", can you please help me?! I'm desperate with twitter's api