Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center text in google sign in button android

Tags:

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

like image 507
Reid Avatar asked Jan 31 '17 21:01

Reid


People also ask

How do I use Google button?

On your device, touch and hold the Home button or say "Hey Google." If the Google Assistant is off, you'll be asked to turn it on. Ask a question or say a command.


1 Answers

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!!.googButton.getChildAt(0)?.let {
        val smaller = Math.min(it.paddingLeft, it.paddingRight)
        it.setPadding(smaller, it.paddingTop, smaller, it.paddingBottom)
    }

That's Kotlin, of course.

like image 105
androidguy Avatar answered Sep 25 '22 11:09

androidguy