Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why drawablePadding is not working in Button?

I am trying to increase gap between the drawable icon on the left and the text on the right. I am using drawablePadding. But this does not seem to have any effect. Here is the code -

<?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="match_parent"
    android:background="@drawable/background_gradient"
    tools:context=".Authentication">

    <Button
        android:id="@+id/google_sign_in_btn"
        android:drawableLeft="@drawable/g_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/white"
        android:fontFamily="@font/roboto_medium"
        android:textColor="#8A000000"
        android:text="@string/sign_in_with_google_txt"
        android:padding="8dp"
        android:drawablePadding="100dp"
        android:textAllCaps="false"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

And here is the result -

enter image description here

like image 570
user1543784 Avatar asked Oct 13 '25 00:10

user1543784


1 Answers

drawablePadding won't work on materialButtons as the materialButton class reads the drawable padding from app:iconPadding attribute instead of drawablePadding.

If you look at the source code of the material button you will see this :

iconPadding = attributes.getDimensionPixelSize(R.styleable.MaterialButton_iconPadding, 0);

here you can see that icon's padding is read from iconPadding attribute. comparing it to TextView (Button extends textView) we have :

     for (int i = 0; i < n; i++) {
          int attr = a.getIndex(i);

         switch (attr) {
         
         //lots of cases

         case com.android.internal.R.styleable.TextView_drawablePadding:
              drawablePadding = a.getDimensionPixelSize(attr, drawablePadding);
              break;

                       }
                                }

To get an idea of how these xml attributes work, see Creating a View Class

like image 153
DrHowdyDoo Avatar answered Oct 14 '25 15:10

DrHowdyDoo