Android Kotlin: CheckBox checked change listener

Android Kotlin - How to implement CheckBox checked change listener

This code demonstrates how to implement a listener for a checkbox's checked state change in an Android application written in Kotlin.

The code consists of two parts: the MainActivity.kt file which handles the logic and the activity_main.xml file which defines the user interface elements.

Breakdown of the Code:

1. MainActivity.kt:

  • This file defines the MainActivity class which extends AppCompatActivity.
  • The onCreate method is responsible for initializing the activity. Inside onCreate:
    • The layout for the activity is inflated using setContentView(R.layout.activity_main).
    • An anonymous listener is assigned to the checkBox using setOnCheckedChangeListener. This listener is triggered whenever the checkbox's checked state changes.
    • The listener takes two arguments: buttonView (which is the checkbox itself) and isChecked (a boolean indicating the current checked state).
    • Inside the listener:
      • If the checkbox is checked (isChecked is true), the text of the textView is updated to "CheckBox is checked." and the font style of the buttonView (checkbox) is set to italic.
      • If the checkbox is unchecked (isChecked is false), the text of the textView is updated to "CheckBox is unchecked." and the font style of the buttonView (checkbox) is set back to normal.

2. activity_main.xml:

  • This file defines the layout for the activity using ConstraintLayout.
  • The layout contains two elements:
    • A CheckBox with the text "Check Me".
    • A TextView displaying a message based on the checkbox state.

Summary

This code showcases a simple implementation of a checkbox checked change listener in Kotlin. By listening to the checkbox state, the application can update the UI and potentially perform other actions based on the user's selection. This demonstrates a fundamental concept for building interactive applications in Android.


MainActivity.kt

package com.example.jetpack

import android.graphics.Typeface
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

        // set checkbox checked change listener
        checkBox.setOnCheckedChangeListener{ buttonView, isChecked ->
            if (isChecked){
                textView.text = "CheckBox is checked."
                buttonView.setTypeface(null,Typeface.ITALIC)
            }else{
                textView.text = "CheckBox is unchecked."
                buttonView.setTypeface(null,Typeface.NORMAL)
            }
        }
    }
}
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"
    android:background="#EDEAE0">

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Check Me"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        tools:text="TextView"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/checkBox" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android kotlin tutorials