Search code examples
androidandroid-layoutgoogle-signintext-alignment

How to center text in google sign in button android


I am using the following code to put the google sign in button in my application. However the text in the button is off center. How can I make it centered?

<com.google.android.gms.common.SignInButton
        android:id="@+id/sign_in_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:visibility="visible"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp" />

enter image description here


Solution

  • By inspecting with the debugger and the decompiled class code, I've noticed:

    • the SignInButton class sets up the real button in setStyle. It uses inter-process binding APIs to create some class from some other library, and I couldn't identify that class nor locate its decompiled class code.
    • the inner button has side paddings set, which is larger on the side with G icon.
    • the G icon is displayed via a background drawable, and not the TextView drawableStart.

    Thus, it seems like some manual padding is put on the real button, on the side with the G icon. Why they would do this, no idea. From the above, it's pretty safe to "fix" the padding after the button is inflated from your XML. I think this is also safe in the case of RTL, since the padding on the side with the icon is always larger, I think (or I sure hope so, somebody please let me know if this is broken in RTL):

        loginBinding!!.googleSignInButton.getChildAt(0)?.let {
            val smaller = Math.min(it.paddingLeft, it.paddingRight)
            it.setPadding(smaller, it.paddingTop, smaller, it.paddingBottom)
        }
    

    That's Kotlin, of course.