android kotlin - Material button center icon only

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.util.TypedValue
import androidx.core.content.ContextCompat
import com.google.android.material.button.MaterialButton


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the context
        val context = this

        // get the widgets reference from XML layout
        val button5 = findViewById<MaterialButton>(R.id.button5)


        // programmatically make button icon only and center of button
        button5.setOnClickListener {
            (it as MaterialButton).apply {
                minimumWidth = 0
                minWidth = 0

                setPadding(
                    5.toDp(context),
                    5.toDp(context),
                    5.toDp(context),
                    5.toDp(context)
                )

                text = ""

                icon = ContextCompat.getDrawable(
                    context,
                    R.drawable.ic_delete_forever
                )

                iconGravity = MaterialButton.ICON_GRAVITY_TEXT_START
                iconPadding = 0
            }
        }
    }
}



// extension method to convert values to dp
fun Int.toDp(context: Context):Int = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    this.toFloat(),
    context.resources.displayMetrics
).toInt()
activity_main.xml

<?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="#DCDCDC"
    android:id="@+id/rootLayout"
    android:padding="24dp">

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:insetLeft="0dp"
        android:insetTop="0dp"
        android:insetRight="0dp"
        android:insetBottom="0dp"
        android:minWidth="0dp"
        android:text=""
        app:icon="@drawable/ic_delete_forever"
        app:iconGravity="textStart"
        app:iconPadding="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:padding="5dp"
        app:icon="@drawable/ic_delete_forever"
        app:iconGravity="textStart"
        app:iconPadding="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:padding="0dp"
        app:icon="@drawable/ic_delete_forever"
        app:iconGravity="textStart"
        app:iconPadding="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:minWidth="0dp"
        android:paddingStart="5dp"
        android:paddingEnd="5dp"
        app:icon="@drawable/ic_delete_forever"
        app:iconGravity="textEnd"
        app:iconPadding="0dp"
        app:iconSize="48dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button3" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Click Me"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button4" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button6"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginTop="32dp"
        android:insetLeft="0dp"
        android:insetTop="0dp"
        android:insetRight="0dp"
        android:insetBottom="0dp"
        android:minWidth="0dp"
        android:text=""
        app:icon="@drawable/ic_delete_forever"
        app:iconGravity="textStart"
        app:iconPadding="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button5" />
        
</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle dependencies

// Material components
implementation 'com.google.android.material:material:1.6.1'
More android examples