TwitterLoginButton does not respond


#1

Hi,
I implemented the TwitterLoginButton as shown on the example on https://dev.twitter.com/twitter-kit/android/twitter-login to allow login into my app using twitter. I am using Android Studio and installed Fabric plugin to get started and registered my app with it. I implemented the code exactly as mentioned but when I click on Twitter button, nothing happens.

My activity Layout is

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splash"
    tools:context="com.rayat.pricewiz.view.activity.SplashScreen">
<FrameLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!--<RelativeLayout android:id="@+id/fullscreen_content_controls"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        tools:ignore="UselessParent" android:visibility="invisible">
            <Button
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:text="Get STARTED"
                android:id="@+id/btnGetStarted"
                android:gravity="center"
                android:layout_marginTop="200dp"
                android:layout_centerHorizontal="true"
                android:textColor="@color/black"
                android:background="@drawable/transparent_rounded_box"
                />
        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="300dp"
            android:layout_height="242dp"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/btnGetStarted"
            android:layout_alignStart="@+id/btnGetStarted"
            android:id="@+id/loginForm"
            android:background="@drawable/login_form">

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/layoutEmail"
            android:weightSum="1">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="20dp"
                    android:layout_weight="0.14"
                    android:src="@drawable/login_form_email_icon"/>

                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="textEmailAddress"
                    android:ems="10"
                    android:layout_marginTop="10dp"
                    android:id="@+id/txtEmailAddress"
                    android:layout_weight="0.72"
                    android:textColor="@color/black"/>
                <TextView
                    android:layout_width="10dp"
                    android:layout_height="10dp"
                    android:layout_weight="0.14"/>
        </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/layoutEmail"
                android:id="@+id/layoutPassword"
                android:layout_alignParentStart="true"
                android:weightSum="1">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imgPassIcon"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="20dp"
                    android:layout_weight="0.14"
                    android:src="@drawable/login_form_passwd_icon"/>

                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="textPassword"
                    android:ems="10"
                    android:layout_marginTop="10dp"
                    android:id="@+id/txtPassword"
                    android:layout_weight="0.72"
                    android:textColor="@color/black"/>
                <TextView
                    android:layout_width="10dp"
                    android:layout_height="10dp"
                    android:layout_weight="0.14"/>
                </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/layoutPassword"
                android:layout_alignParentStart="true"
                android:id="@+id/layoutLang"
                android:weightSum="1">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imgLangIcon"
                    android:layout_marginLeft="3dp"
                    android:layout_marginTop="20dp"
                    android:layout_weight="0.14"
                    android:src="@drawable/login_form_lang_icon"/>

                <Spinner
                    android:layout_height="36dp"
                    android:layout_width="0dp"
                    android:layout_weight="0.80"
                    android:layout_marginTop="14dp"
                    android:spinnerMode="dialog"
                    android:entries="@array/language"
                    android:prompt="@string/select_language"
                    android:id="@+id/selLanguage"
                    style="@style/SpinnerAsEditText"
                    android:background="@null"/>
            </LinearLayout>
            <FrameLayout android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_below="@+id/layoutLang"
                android:layout_marginTop="5dp">
                <Button
                    android:id="@+id/btnLogin"
                    android:layout_width="fill_parent"
                    android:layout_height="36dp"
                    android:scaleType="fitXY"
                    android:background="@drawable/login_selector"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:textColor="@drawable/text_color"
                    android:contentDescription="@string/login"
                    android:onClick="login"
                    android:text="@string/login">
                </Button>
            </FrameLayout>
        </RelativeLayout>
    </RelativeLayout>-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/splashScreenMargin"
        android:layout_marginEnd="@dimen/splashScreenMargin"
        android:layout_centerInParent="true"
        android:id="@+id/socialLoginForm"
        android:background="@drawable/login_form">
        <com.facebook.widget.LoginButton
            xmlns:fb="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:paddingStart="10dp"
            android:id="@+id/btnLoginFacebook"
            android:drawableStart="@drawable/facebook_f"
            />
        <com.twitter.sdk.android.core.identity.TwitterLoginButton
            android:id="@+id/btnTwitterLogin"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_below="@+id/btnLoginFacebook"/>
        <com.google.android.gms.common.SignInButton
            android:id="@+id/btnLogingplus"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_below="@+id/btnTwitterLogin"
            android:text="Log in with Google+"
            />
        <Button android:text="Log in with email"
            android:id="@+id/button5"
            android:background="@drawable/orange_button"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_below="@+id/btnLogingplus"
            android:drawableStart="@drawable/envelope"/>

        </RelativeLayout>
    </RelativeLayout>
</FrameLayout>

Here is how I implemented the login:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
        TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
        Fabric.with(this, new Twitter(authConfig));

        //Twitter login
        twitterButton = (TwitterLoginButton)
                findViewById(R.id.btnTwitterLogin);
        twitterButton.setCallback(new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {
                // Do something with result, which provides a
                // TwitterSession for making API calls
                Log.w("twitter logged","twitter logged");
                TwitterSession session =
                        Twitter.getSessionManager().getActiveSession();
                TwitterAuthToken authToken = session.getAuthToken();
                String token = authToken.token;
                String secret = authToken.secret;
            }

            @Override
            public void failure(TwitterException exception) {
                // Do something on failure
            }
        }); }

When I click on the button, nothing happens and no twitter authorization page appears. I have twitter app installed.

Please help.

Thanks,
Noorul


#2

I’m not sure whether this is relevant, but can you check your manifest includes the correct lines? there is more about this on another forum thread.


#3

Dear Andy,
Thanks for the reply. Yes, I have included the activity in the AndroidManifest.xml. I discovered a peculiar case. I have a different landscape layout for the login activity. My login activity is the Launcher activity and when the app starts, the button remains disabled. It gets enabled if I rotate the phone to landscape and stays enabled when I rotate back to portrait mode.

Here is how I am declaring activity in AndroidManifest.xml

<activity android:name="com.twitter.sdk.android.core.identity.OAuthActivity" >
 </activity>

Thanks,.
Noorul


#4

Hi Noorul,

From the code, it looks like you’re initializing Twitter with Fabric in your Activity’s onCreate method. We recommend you initialize Twitter with Fabric in your Application’s onCreate to ensure that everything is up and running once your Activity is created.

public class MyApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();

    final TwitterAuthConfig authConfig = new TwitterAuthConfig(CONSUMER_KEY, CONSUMER_SECRET);

    Fabric.with(new Twitter(authConfig));
}

Also, you should not have to explicitly declare the com.twitter.sdk.android.core.identity.OAuthActivity directly in your AndroidManifest.xml, as this should automatically get merged when android build tool does manifest merging.

Let us know how it goes!

Lien


#5

Dear Lien,

Thanks, that worked.

Regards,
Noorul


#6