Center text when there is viewStart object in button

Viewed 49

I have a google login button in my activity. I have a google logo in viewStart position on the button. Having it moves the normally centered text off-center.

Button xml:

<Button
        android:id="@+id/signInBtn"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="26dp"
        android:layout_marginEnd="26dp"
        android:layout_marginBottom="108dp"
        android:background="@drawable/roung_bg"
        android:drawableStart="@drawable/ic_google_logo"
        android:elevation="@dimen/elevation"
        android:text="@string/logIn"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

Result: Where text is off-center

Which i am not satisfied with and want to center the "LOG IN" text in layout. How can i achieve this?

2 Answers

You shouldn't use drawbleStart, use two views to change size google logo or position of the text, sample

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorAccent"
    android:layout_marginStart="26dp"
    android:layout_marginEnd="26dp"
    android:layout_marginBottom="108dp">
    <TextView
        android:id="@+id/signInBtn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#FFFFFF"
        android:text="LOG IN"
        android:textColor="#000000"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:gravity="center"
        android:layout_centerInParent="true"/>
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentStart="true"
        android:background="@drawable/ic_google"
        android:layout_centerVertical="true"/>
    
</RelativeLayout>

Create a Relative layout add the button with width match_parent and add the google logo as image in the layout now add android:textAlignment="center" in button and align the logo to left in layout

now you can easily align the button as well as image

Related