CheckedTextView in Kotlin
Last Updated :
12 Jul, 2025
CheckedTextView is an extension of TextView in Android that includes a checkmark, making it function like a checkbox. It is commonly used in list views where items can be selected or toggled between checked and unchecked states. Users can tap the text to change its checked status, and the checkmark updates accordingly.
Steps of Implementation
Step 1: Create a new project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Modify the activity_main.xml file
In this file, we will add the CheckedTextView and use different attributes like checked, gravity etc. Later, it will be called in Kotlin file to add more functionalities.
activity_main.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<CheckedTextView
android:id="@+id/checkedTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:gravity="center"
android:text="CheckedTextViewInKotlin"/>
</LinearLayout>
Step 3: Working with MainActivity.kt file
Here, we first declare a checkedTextView variable and find the xml checkedTextView by using id.
val checkedTextView: CheckedTextView = findViewById(R.id.checkedTextView)
then, check using the conditional statement like
if (checkedTextView.isChecked)
android.R.drawable.checkbox_on_background
else
android.R.drawable.checkbox_off_background
In the end, we declare a variable msg to print the value when we checked the text view.
MainActivity.kt:
Java
package org.geeksforgeeks.demo
import android.os.Bundle
import android.widget.CheckedTextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val checkedTextView: CheckedTextView = findViewById(R.id.checkedTextView)
// Initially set the CheckedTextView to be unchecked
checkedTextView.isChecked = false
// Set the default checkmark icon (unchecked state)
checkedTextView.setCheckMarkDrawable(android.R.drawable.checkbox_off_background)
checkedTextView.setOnClickListener {
// Toggle the checked state (switch between checked and unchecked)
checkedTextView.isChecked = !checkedTextView.isChecked
// Update the checkmark icon based on the new checked state
checkedTextView.setCheckMarkDrawable(
if (checkedTextView.isChecked)
android.R.drawable.checkbox_on_background // Show checked icon
else
android.R.drawable.checkbox_off_background // Show unchecked icon
)
val msg = if (checkedTextView.isChecked)
"View is: checked"
else
"View is: unchecked"
Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show()
}
}
}
Output:
Different attributes of CheckedTextView
| Attributes | Description |
|---|
| android:id | Gives a unique ID to the Textview. |
| android:gravity | We can align text of the Textview vertically or horizontally or both. |
| android:height | Used to set height of the Textview. |
| android:width | Sets width of the TextView. |
| android:padding | Used to set padding. |
| android:checkMark | Used to set the drawable for checkmark. |
| android:checkMarkTint | Used to set tint to the check mark. |
| android:checkMarkTintMode | Blending mode used to apply the check mark tint. |
| android:checked | Used to set the initial checked state of the checkedTextView which is false by default. |
Explore
Overview
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges