I have been trying to use a normal button to execute the authentication process with the twitter sdk but it does not seem to work. Anyone have tried anything similar?
The code
buttonTwitterLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Twitter.logIn(LoginActivity1.this, new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> twitterSessionResult) {
Log.i(TAG, "success");
Log.i(TAG, twitterSessionResult.toString());
}
@Override
public void failure(TwitterException e) {
Log.e(TAG, "failed");
}
});
}
});
You can achieve this by using TwitterAuthClient. i.e,
First of all create normal button like,
<Button
android:id:"@+id/twitter_custom_button"
... />
Now, in you java class file use TwitterAuthClient instead of TwitterLoginButton. then set your CallBack inside Button's onClick
TwitterAuthClient mTwitterAuthClient= new TwitterAuthClient();
Button twitter_custom_button = (Button) findViewById(R.id.twitter_custom_button);
twitter_custom_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTwitterAuthClient.authorize(this, new com.twitter.sdk.android.core.Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> twitterSessionResult) {
// Success
}
@Override
public void failure(TwitterException e) {
e.printStackTrace();
}
});
}
});
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
mTwitterAuthClient.onActivityResult(requestCode, responseCode, intent);
}
Well actually there's a way of doing this
private TwitterAuthClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig));
client = new TwitterAuthClient();
Button customLoginButton = (Button) findViewById(R.id.custom_twitter_login);
customLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
client.authorize(LoginActivity.this, new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> twitterSessionResult) {
Toast.makeText(LoginActivity.this, "success", Toast.LENGTH_SHORT).show();
}
@Override
public void failure(TwitterException e) {
Toast.makeText(LoginActivity.this, "failure", Toast.LENGTH_SHORT).show();
}
});
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
client.onActivityResult(requestCode, resultCode, data);
}
Be aware, onActivityResult part is very important, you seem to lost it.
here's my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/custom_twitter_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/padding_medium"
android:text="@string/twitter_login"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With