Zizmotrex
Zizmotrex

Reputation: 71

Android Studio : Icon next to text in a button

I am developing an app using android studio and I want to add an icon to a text in the button, like this :

button

here is my xml code:

 <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginEnd="5dp"
            android:fontFamily="@font/nunito_bold"
            android:text="@string/conducteur"
            android:textColor="#FFFFFF"
            android:textSize="12sp"
            app:backgroundTint="@color/main_blue"
            app:cornerRadius="20dp" />

Thank you for helping me;

Upvotes: 4

Views: 5310

Answers (2)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363567

You can use the app:icon attribute to define the icon and the iconGravity attribute to define the position.

The default value app:iconGravity="start"

        <com.google.android.material.button.MaterialButton
            app:iconGravity="start"
            android:text="BUTTON"
            app:icon="@drawable/ic_add_24px"/>

or app:iconGravity="textStart"

        <com.google.android.material.button.MaterialButton
            app:iconGravity="textStart"
            android:text="BUTTON"
            app:icon="@drawable/ic_add_24px"/>

enter image description here

Upvotes: 7

tom
tom

Reputation: 11

You could simply use app:icon to specify a drawable for this button :

<com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/conducteur"
        app:icon="@drawable/ic_home"/>

Upvotes: 1

Related Questions