'How do I set strokeColor to MaterialButton programmatically?

How to set the stroke color to MaterialButton programmatically

<com.google.android.material.button.MaterialButton
                    android:layout_weight="1"
                    android:id="@+id/viewQrLayout"
                    style="?attr/materialButtonOutlinedStyle"
                    android:textColor="@color/black"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" View Receipt"

                    app:strokeColor="@color/red"
                    app:strokeWidth="2dp"

                    android:textAllCaps="false"
                    app:icon="@drawable/ic_baseline_qr_code_scanner_24"
                    app:iconTint="@color/color2"
                    android:layout_gravity="start"
                    app:iconPadding="0dp"
                    app:iconSize="25dp"
                    android:letterSpacing=".1"
                    app:cornerRadius="5dp"
                    android:textStyle="bold"
                    android:textAppearance="@style/TextAppearance.AppCompat.Small"
                    />


Solution 1:[1]

To set the stroke color to MaterialButton programmatically

   val colorList = ColorStateList(
            arrayOf(
                intArrayOf(-android.R.attr.state_enabled),  // Disabled
                intArrayOf(android.R.attr.state_enabled)    // Enabled
            ),
            intArrayOf(
                Color.BLACK,     // The color for the Disabled state
                Color.parseColor("#FFBB86FC")        // The color for the Enabled state
            )
        )
        binding!!.viewQrLayout.strokeColor = colorList

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hari Shankar S