Search code examples
androidandroid-layoutfacebook-android-sdk

Facebook LoginButton for Android does not take login_text and logout_text values passed from XML


Facebook Sdk for Android does not take login_text and logout_text values passed from XML. It just ignores it. There is no documentation/example available on earth which uses this customization of the button.

<com.facebook.widget.LoginButton
    xmlns:fb="http://schemas.android.com/apk/res-auto"
    android:id="@+id/connectWithFbButton"
    style="@style/com_facebook_loginview_default_style"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal"
    android:text="@string/connect_with_facebook"
    fb:login_text="@string/connect_with_facebook"
    fb:logout_text="Connecting with facebook" />

It always says Login In or Log Out. How can I use this property? I tried debugging through the LoginButton code but in vain. It never goes through any of the constructors of LoginButton() and hence cannot parse my custom parameters.

Did someone face this issue?


Solution

  • Gabriel is spot on. To fix this, go to LoginButton.java in Facebook SDK and move the parseAttributes call

    parseAttributes(attrs)
    

    to the bottom so that it always runs.

    Whole method will look like this :

    public LoginButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        if (attrs.getStyleAttribute() == 0) {
                // apparently there's no method of setting a default style in xml,
                // so in case the users do not explicitly specify a style, we need
                // to use sensible defaults.
                this.setTextColor(getResources().getColor(R.color.com_facebook_loginview_text_color));
                this.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        getResources().getDimension(R.dimen.com_facebook_loginview_text_size));
                this.setPadding(getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_left),
                        getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_top),
                        getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_right),
                        getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_bottom));
                this.setWidth(getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_width));
                this.setHeight(getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_height));
                this.setGravity(Gravity.CENTER);
    
            if(isInEditMode()) {
                // cannot use a drawable in edit mode, so setting the background color instead
                // of a background resource.
                this.setBackgroundColor(getResources().getColor(R.color.com_facebook_blue));
                // hardcoding in edit mode as getResources().getString() doesn't seem to work in IntelliJ
                loginText = "Log in";
            } else {
                this.setBackgroundResource(R.drawable.com_facebook_loginbutton_blue);
                initializeActiveSessionWithCachedToken(context);
            }
        } 
        parseAttributes(attrs);
    }
    

    both style and login_text are taken under consideration now. Works fine for me.