Search code examples
androidandroid-layoutbuttonandroid-drawableandroid-button

How to reduce the spacing between Button text and icon within a button?


enter image description here

I put an image (icon) and text inside the button like the picture.

I want to move the image in the button next to the text, but I can't use padding and I can't use the drawableLeft attribute.

Any good way?

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    app:cardElevation="10dp">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/ll1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintTop_toTopOf="parent">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:text="TEST"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@color/light_blue_400"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintTop_toBottomOf="@+id/ll1">
            <Button
                android:id="@+id/delete"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="4dp"
                app:icon="@drawable/ic_delete"
                android:text="delete"/>

            <Button
                android:id="@+id/add"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="4dp"
                app:icon="@drawable/ic_add"
                android:text="add"/>
        </LinearLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/ll2"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

Solution

  • Here is a solution by manipulating the padding:

    • Adjust negative padding to app:iconPadding

    • Adjust positive padding to android:paddingStart

       <Button
           android:id="@+id/delete"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_marginStart="8dp"
           android:layout_marginEnd="4dp"
           android:layout_weight="1"
           android:backgroundTint="@color/black"
           android:paddingStart="60dp"
           android:paddingLeft="60dp"
           android:text="delete"
           android:textColor="@color/white"
           app:icon="@drawable/ic_baseline_remove_24"
           app:iconPadding="-40dp"
           app:iconTint="@color/white" />
      
       <Button
           android:id="@+id/add"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_marginStart="4dp"
           android:layout_marginEnd="8dp"
           android:layout_weight="1"
           android:backgroundTint="@color/black"
           android:paddingStart="50dp"
           android:paddingLeft="50dp"
           android:text="add"
           android:textColor="@color/white"
           app:icon="@drawable/ic_baseline_add_24"
           app:iconPadding="-50dp"
           app:iconTint="@color/white" />
      

    Preview:

    enter image description here