MainActivity.kt
package com.cfsuman.kotlintutorials
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.button.MaterialButton
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get the widgets from XML layout
val btnClick = findViewById<MaterialButton>(R.id.btnClick);
// Create a color state list programmatically
val states = arrayOf(
intArrayOf(android.R.attr.state_enabled), // enabled
intArrayOf(-android.R.attr.state_enabled) // disabled
)
val colors = intArrayOf(
Color.parseColor("#545AA7"), // enabled color
Color.parseColor("#E6E6FA") // disabled color
)
val colorStates = ColorStateList(states,colors)
// Set button background tint
btnClick.backgroundTintList = colorStates
// Disable button
btnClick.setOnClickListener {
(it as MaterialButton).apply {
isEnabled = false
}
}
}
}
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
android:background="#F8F8F8">
<com.google.android.material.button.MaterialButton
android:id="@+id/btnNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Material Button Normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnDisabled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:enabled="false"
android:text="Material Button Disabled"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnNormal" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnTint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:backgroundTint="@color/color_state_material_button"
android:text="Tint Red + Normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnDisabled" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnTintDisabled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:backgroundTint="@color/color_state_material_button"
android:text="Tint Red + Disabled"
android:enabled="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnTint" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Programmatically Tint + Click To Disable"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnTintDisabled" />
</androidx.constraintlayout.widget.ConstraintLayout>
res/color/color_state_material_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:color="#D3212D" />
<item
android:state_enabled="false"
android:color="#DBE1E4"
/>
</selector>