VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/checkedtextview-in-kotlin/

⇱ CheckedTextView in Kotlin - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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:


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:

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.
Comment
Article Tags:

Explore