How to center icon in a MaterialButton which has no text?

AndroidMaterial ComponentsMaterial Components-Android

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

Change iconGravity to textStart and iconPadding to 0dp

    app:iconPadding="0dp"
    android:padding="0dp"
    app:iconGravity="textStart"

Solution 3 - Android

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

Solution 4 - Android

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.

Solution 5 - Android

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

Solution 6 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdil HussainView Question on Stackoverflow
Solution 1 - AndroidAdil HussainView Answer on Stackoverflow
Solution 2 - AndroidKouroshView Answer on Stackoverflow
Solution 3 - AndroidBouhView Answer on Stackoverflow
Solution 4 - AndroidCameron KetchamView Answer on Stackoverflow
Solution 5 - AndroidWirlingView Answer on Stackoverflow
Solution 6 - AndroidFreePhoenixView Answer on Stackoverflow