74

I'm using the latest version of the com.google.android.material:material library (i.e. 1.1.0-alpha03) and I have a MaterialButton defined with an icon and no text as follows:

I was hoping the MaterialButton would be rendered as a square with the icon centred within it but instead the MaterialButton is rendered as follows:

MaterialButton with iconGravity value of start

If I change the iconGravity value to "textStart" the MaterialButton is rendered as follows:

MaterialButton with iconGravity value of textStart

This is a slight improvement to the positioning of the icon but the icon is still a little off centre. If I change the insetLeft, insetRight, insetTop and insetBottom values to 0dp the MaterialButton is rendered as follows:

enter image description here

This is an improvement to the shape of the button but the icon is still a little off centre.

Anyone know whether there's something further I can do to centre the icon within the MaterialButton?

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
  • Possibly duplicate of https://stackoverflow.com/questions/51633118/how-to-set-materialbutton-icon-to-the-center I may recommend to use "textStart" and insetLeft=2dp (may be 1-4dp, so look what you've got while changes) – Raskilas Feb 15 '19 at 16:18
  • Thank you for pointing that thread out. I had seen that thread but it's not the same problem. That thread (from what I understood of it) was about centering the icon together with the text within the `MaterialButton`. I am trying to centre an icon which has no supporting text. I found my answer nonetheless. The missing attribute I needed was `app:iconPadding="0dp"`. – Adil Hussain Feb 15 '19 at 16:37
  • https://material.io/components/buttons/android#toggle-button – Naveen Kumar Jul 06 '21 at 16:45

9 Answers9

154

Found it. The attribute I was missing was app:iconPadding="0dp".

So, from my experiments, the minimum attributes needed to create a square MaterialButton which has a centred icon and no text is the following:

<com.google.android.material.button.MaterialButton
    android:layout_width="52dp"
    android:layout_height="52dp"
    android:insetLeft="0dp"
    android:insetTop="0dp"
    android:insetRight="0dp"
    android:insetBottom="0dp"
    app:icon="@drawable/icon_next"
    app:iconGravity="textStart"
    app:iconPadding="0dp" />

These attributes produce a MaterialButton as follows:

MaterialButton with square shape and centred icon

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
64

Change iconGravity to textStart and iconPadding to 0dp

    app:iconPadding="0dp"
    android:padding="0dp"
    app:iconGravity="textStart"
Kourosh
  • 2,239
  • 13
  • 18
12

Use Widget.Material3.Button.IconButton style

<com.google.android.material.button.MaterialButton
    style="@style/Widget.Material3.Button.IconButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:icon="@drawable/ic_baseline_power_settings_new_24"
    app:backgroundTint="?android:attr/colorButtonNormal"
    android:layout_gravity="center"
    />

enter image description here

FreePhoenix888
  • 4,510
  • 3
  • 13
  • 22
10

Add this style to the button

<Button style="@style/Widget.App.Button.IconOnly"/>

then in res/values/styles.xml add the following

<style name="Widget.App.Button.IconOnly" parent="Widget.MaterialComponents.Button">
    <item name="iconPadding">0dp</item>
    <item name="android:insetTop">0dp</item>
    <item name="android:insetBottom">0dp</item>
    <item name="android:paddingLeft">12dp</item>
    <item name="android:paddingRight">12dp</item>
    <item name="android:minWidth">48dp</item>
    <item name="android:minHeight">48dp</item>
</style>

source: https://material.io/components/buttons/android

Bouh
  • 1,303
  • 2
  • 10
  • 24
1

There is also an Icon version of the material style on your button. For filled buttons like you are using, you could set the style to Widget.MaterialComponents.Button.Icon.

Cameron Ketcham
  • 7,966
  • 2
  • 28
  • 27
  • Thanks for the suggestion. I've just tried this but I've found it's still necessary to set the `insetLeft`, `insetTop`, `insetRight`, `insetBottom`, `iconGravity` and `iconPadding` attributes on the `MaterialButton` as described in my answer to get a square shape with a centred icon. If you can share an example of the attributes that you've used together with the style attribute and a screenshot of the result that would be handy! – Adil Hussain Feb 21 '19 at 19:22
1

I noticed that app:iconGravity="textStart" stopped working after I updated appcompat to version 1.4.0. I now use app:iconGravity="textTop"

Wirling
  • 4,810
  • 3
  • 48
  • 78
1

using Widget.Material3.Button.OutlinedButton.Icon style :

style="@style/Widget.Material3.Button.OutlinedButton.Icon"
app:icon="@drawable/ic_baseline_close_24"
app:iconGravity="textStart"
app:iconPadding="0dp"
android:padding="10dp"
android:minWidth="0dp"
Raphael C
  • 2,296
  • 1
  • 22
  • 22
1

I was able to achieve this in code with:

button.setIconPadding(0);
button.setPaddingRelative(0, 0, 0, 0);
button.setIconGravity(MaterialButton.ICON_GRAVITY_TEXT_TOP);
JustSightseeing
  • 1,460
  • 3
  • 17
  • 37
0

square button:

 <com.google.android.material.button.MaterialButton
        style="@style/Widget.Material3.Button.Icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        app:iconGravity="textTop"
        android:padding="0dp"
        app:cornerRadius="16dp"
        app:iconPadding="0dp"
        android:insetTop="0dp"
        android:insetBottom="0dp"
        android:insetLeft="0dp"
        android:insetRight="0dp"
        app:backgroundTint="#30000000"
        app:iconTint="@color/white"
        app:icon="@drawable/bag"/>

circle :

<com.google.android.material.button.MaterialButton
            style="@style/Widget.MaterialComponents.Button.Icon"
            android:layout_width="40dp"
            android:layout_height="40dp"
            app:cornerRadius="20dp"
            app:iconGravity="textStart"
            app:iconPadding="0dp"
            android:insetTop="0dp"
            android:insetBottom="0dp"
            android:insetLeft="0dp"
            android:insetRight="0dp"
            app:backgroundTint="@color/white"
            app:iconTint="@color/colorPrimary"
            app:icon="@drawable/send"/>
Azade Rahmati
  • 135
  • 1
  • 5