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 extendsAppCompatActivity
. - The
onCreate
method is responsible for initializing the activity. InsideonCreate
:- The layout for the activity is inflated using
setContentView(R.layout.activity_main)
. - An anonymous listener is assigned to the
checkBox
usingsetOnCheckedChangeListener
. This listener is triggered whenever the checkbox's checked state changes. - The listener takes two arguments:
buttonView
(which is the checkbox itself) andisChecked
(a boolean indicating the current checked state). - Inside the listener:
- If the checkbox is checked (
isChecked
is true), the text of thetextView
is updated to "CheckBox is checked." and the font style of thebuttonView
(checkbox) is set to italic. - If the checkbox is unchecked (
isChecked
is false), the text of thetextView
is updated to "CheckBox is unchecked." and the font style of thebuttonView
(checkbox) is set back to normal.
- If the checkbox is checked (
- The layout for the activity is inflated using
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.
- A
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>
- android kotlin - Set CardView elevation
- android kotlin - Set CardView background color
- android kotlin - Set CardView corner radius
- android kotlin - RadioButton with image and text
- android kotlin - RadioButton circle color programmatically
- android kotlin - RadioButton onClick event
- android kotlin - TextView margin programmatically
- android kotlin - TextView html formatted text
- android kotlin - TextView add border programmatically
- android kotlin - Coroutines with timeout
- android kotlin - Coroutines JSON from URL
- android kotlin - Room with coroutines
- android kotlin - Coroutines async await all
- android kotlin - Coroutines async
- android kotlin - Canvas draw circle