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");
}
});
}
});
Luis from Developer Relations team at Twitter. Fabric will support theming on the future, meanwhile you can customize the button by creating a custom view that inherits from TwitterLoginButton
.
Cannonball sample app implements a custom button:
public class CannonballTwitterLoginButton extends TwitterLoginButton {
public CannonballTwitterLoginButton(Context context) {
super(context);
init();
}
public CannonballTwitterLoginButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CannonballTwitterLoginButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
if (isInEditMode()){
return;
}
setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable
.ic_signin_twitter), null, null, null);
setBackgroundResource(R.drawable.sign_up_button);
setTextSize(20);
setPadding(30, 0, 10, 0);
setTextColor(getResources().getColor(R.color.tw__blue_default));
setTypeface(App.getInstance().getTypeface());
}
}
Layout file:
<io.fabric.samples.cannonball.view.CannonballTwitterLoginButton
android:id="@+id/twitter_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/sign_up_button"
android:layout_marginTop="@dimen/login_button_margin_top"
android:layout_marginBottom="@dimen/login_button_margin_bottom"
android:layout_marginLeft="@dimen/login_button_margin_start"
android:layout_marginStart="@dimen/login_button_margin_start"
android:layout_marginRight="@dimen/login_button_margin_end"
android:layout_marginEnd="@dimen/login_button_margin_end"
android:text="@string/sign_in_with_twitter"
android:textColor="@color/grayish_blue"
android:textSize="@dimen/login_button_text_size"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
Callback setup:
private void setUpTwitterButton() {
twitterButton = (TwitterLoginButton) findViewById(R.id.twitter_button);
twitterButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
SessionRecorder.recordSessionActive("Login: twitter account active", result.data);
startThemeChooser();
}
@Override
public void failure(TwitterException exception) {
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.toast_twitter_signin_fail),
Toast.LENGTH_SHORT).show();
Crashlytics.logException(exception);
}
});
}
I strongly recommend you to clone the code and take a look on it.