android kotlin - MaterialCardView checked unchecked example

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import androidx.core.content.ContextCompat
import com.google.android.material.card.MaterialCardView


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 materialCardView1 = findViewById<MaterialCardView>(
            R.id.materialCardView1)
        val materialCardView2 = findViewById<MaterialCardView>(
            R.id.materialCardView2)


        // programmatically check uncheck material card view
        materialCardView1.setOnClickListener {
            (it as MaterialCardView).apply {
                // toggle card checked status
                isChecked = !isChecked
            }
        }


        // programmatically make card checkable
        materialCardView2.apply {
            isCheckable = true
            checkedIcon = ContextCompat.getDrawable(
                context,
                R.drawable.ic_action_checkbox
            )
            checkedIconTint = ColorStateList.valueOf(
                Color.BLUE
            )
        }


        // set click listener for material card view
        materialCardView2.setOnClickListener {
            (it as MaterialCardView).apply {
                // toggle checked status
                isChecked = !isChecked
            }
        }
    }
}
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.card.MaterialCardView
        android:id="@+id/materialCardView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checkable="true"
        app:cardBackgroundColor="#D6D2C3"
        app:cardElevation="12dp"
        app:checkedIcon="@drawable/ic_action_checkbox"
        app:checkedIconTint="#503E03"
        app:contentPadding="32dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <com.google.android.material.textview.MaterialTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:text="Click to check uncheck card."
            android:textSize="24sp" />
    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/materialCardView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp"
        app:cardBackgroundColor="#8F9779"
        app:cardElevation="12dp"
        app:contentPadding="32dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/materialCardView1">
        <com.google.android.material.textview.MaterialTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:text="Programmatically make checkable."
            android:textSize="24sp" />
    </com.google.android.material.card.MaterialCardView>

</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle dependencies

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