Open In App

CheckedTextView in Kotlin

Last Updated : 24 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

AttributesDescription
android:idGives a unique ID to the Textview.
android:gravityWe can align text of the Textview vertically or horizontally or both.
android:heightUsed to set height of the Textview.
android:widthSets width of the TextView.
android:paddingUsed to set padding.
android:checkMarkUsed to set the drawable for checkmark.
android:checkMarkTintUsed to set tint to the check mark.
android:checkMarkTintModeBlending mode used to apply the check mark tint.
android:checkedUsed to set the initial checked state of the checkedTextView which is false by default.


Next Article

Similar Reads