28

I've got a button and a drawable on the left of the text, but I want the drawable to be closer to the text. So I need to move the drawable.

I've defined android:drawableLeft but the content of the button is not centered.

Here is my code:

<Button
    android:id="@+id/addsubject"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/ic_action_add"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:text="@string/addsubject"
    android:textColor="@color/white"
    android:background="@drawable/custombutton1" />

Here is how it looks now:

enter image description here

And here is how I'd like it to be:

enter image description here

Thank you!

Isaías
  • 491
  • 2
  • 9
  • 20

8 Answers8

18

try this

 <Button
    android:id="@+id/addsubject"
    android:layout_width="160dip"
    android:layout_height="60dip"
    android:layout_gravity="center"
    android:drawableLeft="@drawable/ic_action_add"
    android:drawablePadding="2dip"
    android:gravity="center"
    android:paddingLeft="30dip"
    android:paddingRight="26dip"
    android:singleLine="true"
    android:text="@string/addsubject"
    android:textSize="13dip" />
Rakesh Rangani
  • 1,039
  • 10
  • 13
18

Use the app:iconGravity="textStart" attribute in the MaterialButton

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

enter image description here

If you want to reduce the padding between the icon and the text just use the app:iconPadding attribute:

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

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • your answer will never work until you apply Material component theme to the button like this android:theme="@style/Theme.MaterialComponents.Light" – Michael Mar 29 '23 at 13:47
6

I got this result by simply putting these properties to the button specifically

<Button
    ...
    android:paddingLeft="60dp"
    android:drawablePadding="-50dp"
    android:gravity="center"
    android:drawableLeft="@drawable/your_button_icon"
    /> 

You can tune in with your own values for your desired results.

Cheers :-)

Imtiaz Abir
  • 731
  • 7
  • 12
  • Thanks, it works. But delete `android:padding` if you have it. In other case an icon will stand left and a text will be centered. Also result depends on a width of a button. – CoolMind Feb 28 '18 at 09:25
  • The solution is bad as this way the button will look differently on different screen sizes. – dephinera Mar 07 '18 at 14:48
  • @definera can you please propose a better solution for this view(button) which can adapt different screen sizes? – Imtiaz Abir Mar 08 '18 at 09:26
  • 1
    @ImtiazAbir Perhaps using SpannableString with ImageSpan is an option. Haven't tried it yet personally, so I can't give a specific example. – dephinera Mar 08 '18 at 11:36
3

I done this simply using paddingLeft and paddingRight. Its works! ... android:paddingLeft="25dp" android:paddingRight="25dp"/>

seven star
  • 31
  • 1
1

The padding between the left border of button and right border of the icon is controlled by android:paddingLeft property. The padding between the icon and the text is defined by android:drawablePadding property.

Add these two properties to you button and set the values you are happy with.

<Button
    ...
    android:paddingLeft="24dp"
    android:drawablePadding="8dp"
    />
sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
1

You can use LinearLayout as your button by adding OnClickListener and customize however you want.

<LinearLayout          
    android:layout_width="160dip"
    android:layout_height="60dip"
    android:orientation="horizontal" 
    android:background="@drawable/custombutton1">

    <ImageView android:layout_height="wrap_content"
               android:layout_width="wrap_content"
               android:src="@drawable/ic_action_add"
               android:layout_gravity="center"
               android:layout_weight="1"
               android:scaleType="center">

    </ImageView>

    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/addsubject"
              android:layout_gravity="center"
              android:layout_weight="1"/>

</LinearLayout>
Pamir Cevikogullari
  • 482
  • 1
  • 6
  • 10
0

There's also other possibilities that you want to inflate the drawable/icon programatically, after reading a while and using a lot of combination using XMLs, but still doesn't works,

You might want to fork below library instead, to suits your use-cases, such as, customizing the view, position, etc programatically, see below reference:

  1. Drawable/icon aligned with text, see sample here
  2. Button like facebook sign-in, see sample here
  3. For RadioButton, see full gist code here

P.S: Copyrighted and regards to the author

mochadwi
  • 1,190
  • 9
  • 32
  • 87
0

for Materialbutton, you have to use the icon attribute and you can also change its tint with iconTint like this example:

<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:icon="@drawable/ic_cancel"
    app:iconTint="@color/black"
    android:text="text" />
Amin Keshavarzian
  • 3,646
  • 1
  • 37
  • 38