Saturday, March 7, 2020

android kotlin - MaterialCardView checked unchecked example

MainActivity.kt

package com.example.jetpack

import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.card.MaterialCardView
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

        // 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 = getDrawable(R.drawable.ic_check_box)
            checkedIconTint = ColorStateList.valueOf(Color.RED)
        }

        // 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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/materialCardView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        app:cardBackgroundColor="#EDEAE0"
        app:contentPadding="16dp"
        app:cardElevation="12dp"
        android:checkable="true"
        app:checkedIcon="@drawable/ic_check_circle"
        app:checkedIconTint="#E52B50"
        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:text="Click to check uncheck card."
            android:textAppearance=
                "@style/TextAppearance.MaterialComponents.Headline3"
            />
    </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_marginStart="16dp"
        android:layout_marginTop="36dp"
        android:layout_marginEnd="16dp"
        app:cardBackgroundColor="#8F9779"
        app:contentPadding="16dp"
        app:cardElevation="12dp"
        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:text="Programmatically make checkable."
            android:textAppearance=
                "@style/TextAppearance.MaterialComponents.Headline4"
            />
    </com.google.android.material.card.MaterialCardView>

</androidx.constraintlayout.widget.ConstraintLayout>
More android examples