Skip to content
Advertisement

Android studio – How to set text position on center when using drawableLeft property on a Button?

How to set text set on center position when using DrawableLeft on a button?

<Button
        android:layout_marginTop="80dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:background="@drawable/rectangle_round_white"
        android:drawableLeft="@drawable/icon_google"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:elevation="5dp"
        android:text="start with google"
        android:textAlignment="center"
        android:textSize="11dp"/>

enter image description here

the text START WITH GOOGLE is not placed on the center. How to center it?

Advertisement

Answer

One hacky solution is to make a transparent png file or the same color as the background with the exact dimensions of icon_google and set it as drawableRight like so:

<Button
        android:layout_marginTop="80dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:background="@drawable/rectangle_round_white"
        android:drawableLeft="@drawable/icon_google"
        android:drawableRight="@drawable/transparent_icon_google"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:elevation="5dp"
        android:text="start with google"
        android:textAlignment="center"
        android:textSize="11dp"/>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement