Facebook SDK v4 LoginButton ignores XML customizations

前端 未结 6 878
抹茶落季
抹茶落季 2020-12-13 00:36

The new Facebook SDK for Android (v4.0) that was released recently caused strange behavior for a customized LoginButton I\'m using. Below is a comparison of how the same XML

相关标签:
6条回答
  • 2020-12-13 00:45

    Height of LoginButton is related to his paddings and text size:

    //LoginButton.java
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = (getCompoundPaddingTop() +
                (int)Math.ceil(Math.abs(fontMetrics.top) + Math.abs(fontMetrics.bottom)) +
                getCompoundPaddingBottom());
        //...
    }
    

    So if you want to change its height via xml file use android:padding* and android:textSize properties or create style for it:

    <style name="FacebookLoginButtonStyle">
        <item name="android:textSize">16sp</item>
        <item name="android:paddingTop">10sp</item>
        <item name="android:paddingBottom">10sp</item>
    </style>
    
    0 讨论(0)
  • 2020-12-13 00:51

    Because I wanted to customize the login text gravity start, I met a problem with the setCompoundDrawablePadding method. Finally I solved it by using a custom layout. I think it's a much easier way to customize the Facebook login button.

    Here's the end result:

    The layout xml:

    <LinearLayout
            android:id="@+id/fb_login_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/com_facebook_button_background"
            android:orientation="horizontal">
    
            <ImageView
                android:layout_width="38dp"
                android:layout_height="38dp"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginStart="1dp"
                android:layout_marginTop="1dp"
                android:padding="8dp"
                android:src="@drawable/com_facebook_button_icon" />
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:padding="8dp"
                android:text="Log in with Facebook"
                android:textColor="@color/white"
                android:textSize="14sp"
                android:textStyle="bold" />
        </LinearLayout>
    

    Java code that handles the custom Facebook login button clicks:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    
        findViewById(R.id.fb_login_btn).setOnClickListener(this);
        boolean login = AccessToken.getCurrentAccessToken() != null;
        updateFacebookLogInButton(login);
    
        new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(
                    AccessToken oldAccessToken,
                    AccessToken currentAccessToken) {
                if (currentAccessToken == null) {
                    updateFacebookLogInButton(false);
                }
            }
        };
    
        FacebookCallback<LoginResult> facebookCallback = new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                updateFacebookLogInButton(true);
                handleLoginResult(loginResult.getAccessToken());
            }
    
            @Override
            public void onCancel() {
    
            }
    
            @Override
            public void onError(FacebookException error) {
                error.printStackTrace();
            }
        };
        callbackManager = CallbackManager.Factory.create();
    
        loginManager = LoginManager.getInstance();
        loginManager.registerCallback(callbackManager, facebookCallback);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fb_login_btn:
                if (AccessToken.getCurrentAccessToken() == null) {
                    loginManager.logInWithReadPermissions(this, Arrays.asList("public_profile", "email"));
                } else {
                    String logout = getResources().getString(
                            com.facebook.R.string.com_facebook_loginview_log_out_action);
                    String cancel = getResources().getString(
                            com.facebook.R.string.com_facebook_loginview_cancel_action);
                    String message;
                    Profile profile = Profile.getCurrentProfile();
                    if (profile != null && profile.getName() != null) {
                        message = String.format(
                                getResources().getString(
                                        com.facebook.R.string.com_facebook_loginview_logged_in_as),
                                profile.getName());
                    } else {
                        message = getResources().getString(
                                com.facebook.R.string.com_facebook_loginview_logged_in_using_facebook);
                    }
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage(message)
                            .setCancelable(true)
                            .setPositiveButton(logout, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    loginManager.logOut();
                                }
                            })
                            .setNegativeButton(cancel, null);
                    builder.create().show();
                }
                break;
            default:
                break;
        }
    }
    
    private Bundle getRequestParameters() {
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,email,gender");
        return parameters;
    }
    
    private void updateFacebookLogInButton(boolean login) {
        TextView loginTextView = (TextView) findViewById(R.id.fb_login_tv);
        if (login) {
            loginTextView.setText("Log out");
        } else {
            loginTextView.setText("Log in with Facebook");
        }
    }
    
    private void handleRequestResult(JSONObject object) {
        // handle GraphRequest here
    }
    
    0 讨论(0)
  • 2020-12-13 00:54

    I managed to get the desired result by following the following steps:

    1. Opened the Facebook SDK 3.x LoginButton code and saw how the button was styled there:

      this.setBackgroundResource(R.drawable.com_facebook_button_blue);
      this.setCompoundDrawablesWithIntrinsicBounds(
                   R.drawable.com_facebook_inverse_icon, 0, 0, 0);
      this.setCompoundDrawablePadding(getResources().getDimensionPixelSize(
                   R.dimen.com_facebook_loginview_compound_drawable_padding));
      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));
      
    2. Based on the solution presented in this answer, I changed the button parameters during onPostCreate() as follows:

      float fbIconScale = 1.45F;
      Drawable drawable = hostActivity.getResources().getDrawable(
                                     com.facebook.R.drawable.com_facebook_button_icon);
      drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*fbIconScale),
                               (int)(drawable.getIntrinsicHeight()*fbIconScale));
      authButton.setCompoundDrawables(drawable, null, null, null); 
      authButton.setCompoundDrawablePadding(hostActivity.getResources().
                        getDimensionPixelSize(R.dimen.fb_margin_override_textpadding));
      authButton.setPadding(
              hostActivity.getResources().getDimensionPixelSize(
                                                        R.dimen.fb_margin_override_lr),
              hostActivity.getResources().getDimensionPixelSize(
                                                       R.dimen.fb_margin_override_top),
              hostActivity.getResources().getDimensionPixelSize(
                                                        R.dimen.fb_margin_override_lr),
              hostActivity.getResources().getDimensionPixelSize(
                                                   R.dimen.fb_margin_override_bottom));
      

      Where my custom dimensions are as follows:

      <dimen name="fb_margin_override_top">13dp</dimen>
      <dimen name="fb_margin_override_bottom">13dp</dimen>
      <!--The next value changes the margin between the FB icon and the left border:-->
      <dimen name="fb_margin_override_lr">10dp</dimen>
      <!--The next value changes the margin between the FB icon and the login text:-->
      <dimen name="fb_margin_override_textpadding">17dp</dimen>
      

    This results in the desired layout:

    The desired layout :D

    0 讨论(0)
  • 2020-12-13 00:56

    People, u need only set paddingTop and paddingBottom values.

                android:paddingTop="15dp"
                android:paddingBottom="15dp"
    
    0 讨论(0)
  • 2020-12-13 01:10

    i faced the same issue and i solved it by setting the padding and the drawables in java code like this:

    authButton.setPadding(0, myTopDp, 0, myBottomDp);
        authButton.setCompoundDrawablePadding(hostActivity.getResources().getDimensionPixelSize(R.dimen.fb_margin_override_textpadding));
        authButton.setCompoundDrawablesWithIntrinsicBounds(myFbResource, 0, 0, 0);
    

    or if you use your image as drawable

    authButton.setCompoundDrawablesWithIntrinsicBounds(myFbDrawable, null, null, null);
    

    I believe the OnPostCreate method is not required.

    0 讨论(0)
  • 2020-12-13 01:11

    Just put the button into a RelativeLayout. My Height is 60dp

     <RelativeLayout
        android:id="@+id/rlGoogle"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="30dp"
        app:layout_constraintTop_toBottomOf="@+id/btnSingInWithFace">
    
        <com.google.android.gms.common.SignInButton
            android:id="@+id/btnSingInWithGoogle"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题