Skip to content
Related Articles
Open in App
Not now

Related Articles

CheckedTextView in Kotlin

Improve Article
Save Article
  • Last Updated : 18 Feb, 2021
Improve Article
Save Article

CheckedTextView is used to implement checkable interface where one can tick or check the needed or required items and leave out the rest. In this article, we will be discussing how to make a CheckedTextView manually.

The first step is to make or create a project in Android Studio. Here, we will be creating a project named CheckedTextViewInKotlin.

For creating a new project:

  1. Click on File, then New => New Project
  2. Then, check Include Kotlin Support and click next button.
  3. Select minimum SDK, whatever you need.
  4. Select Empty activity and then click finish.

Different attributes of CheckedTextView:

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.

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.

<?xml version="1.0" encoding="utf-8"?>

Make some changes in strings.xml file like app_name and other strings used in Kotlin file.

    <string name="app_name">CheckedTextViewInKotlin</string>
    <string name="msg_shown">CTView is:</string>
    <string name="checked">checked</string>
    <string name="unchecked">unchecked</string>
    <string name="checkedTextView">CheckedTextView</string>

MainActivity.kt file

Here, we first declare a checkedTextView variable and find the xml checkedTextView by using id.

val CTView = findViewById(

then, check using the conditional statement like

if (CTView.isChecked)

In the end, we declare a variable msg to print the value when we checked the text view.

package com.geeksforgeeks.myfirstkotlinapp
import android.os.Bundle
import android.widget.CheckedTextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val CTView = findViewById<CheckedTextView>(
        if (CTView != null) {
            CTView.isChecked = false
            CTView.setOnClickListener {
                CTView.isChecked = !CTView.isChecked
                    if (CTView.isChecked)
                val msg = getString(R.string.msg_shown)+ " " +
                        getString(if (CTView.isChecked)
                            R.string.checked else R.string.unchecked)
                Toast.makeText(this@MainActivity, msg,

AndroidManifest.xml file

This file contains the information like app_name specified in the strings.xml and other important android information.

<?xml version="1.0" encoding="utf-8"?>
    <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

Run as emulator:


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!