How to Align Drawable / Icon to Right of Text in a Button?

91 Views Asked by At

In my Android app, I want to have a drawable ( or icon) immediately to the right of the button's text, but I'm facing an issue where the drawable is too far from the text, and the app:icon attribute is not visible at all.

Here's the XML code I'm currently using:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
  

<com.google.android.material.button.MaterialButton
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:drawableRight="@drawable/caret_down"
        android:drawablePadding="20dp"
        android:gravity="center"
        android:minWidth="78dp"
        android:minHeight="60dp"
        android:textAlignment="center"
        app:icon="@drawable/caret_down"
        app:iconGravity="textEnd"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="lorem" />
</androidx.constraintlayout.widget.ConstraintLayout>

I use com.google.android.material:material:1.7.0

Actual result: actual

Expected: Expected

As you can see I tried every xml attribute I can think of. On the screen shot the pointer is drawable, not icon. For some reason, icon is not visible at all (I tried adding material style, yes). I tried it with MaterialButton, Button and AppCompatButton - same result. Button's width has to stay wide.

What am I doing wrong, and how can I achieve the desired layout where the drawable is closer to the text?

2

There are 2 best solutions below

0
Eeman On

You are using too much properties. Below is Working properly in my system:

 <com.google.android.material.button.MaterialButton
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:text="lorem"
    app:icon="@drawable/baseline_send_24"
    app:iconGravity="textEnd"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

I checked this on two version of material design ie material:1.9.0 and on material:1.7.0. Showing perfectly on both versions.

Try removing your code and paste this one.

0
Paul Sizon On

Turns out, my code above actually works. The issue was with Android's studio xml preview