Open In App
Related Articles

Dynamic CheckedTextView in Kotlin

Like Article
Save Article
Report issue
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 dynamically or programmatically. The first step is to make or create a project in Android Studio. Here, we will be creating a project named DynamicCheckedTextView. For creating a new project:
  • Click on File, then New => New Project
  • Then, check Include Kotlin Support and click next button.
  • Select minimum SDK, whatever you need.
  • Select Empty activity and then click finish.
Now, we need to modify our layout. For doing so : Go to app > res > layout and paste the following code:

Modify the activity_main.xml file

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

Next step is to add the strings which will be displayed when we check or uncheck our CheckedTextView. Go to res/values/strings.xml and add the following lines.
    <string name="app_name">DynamicCheckedTextView</string>
    <string name="checked">checked</string>
    <string name="unchecked">unchecked</string>
    <string name="pre_msg">TextView is</string>


Use CheckedTextView Code in MainActivity.kt file

The final step is to code our CheckedTextView. Open app/src/main/java/yourPackageName/MainActivity.kt
package com.geeksforgeeks.myfirstkotlinapp
import android.os.Bundle
import android.view.ViewGroup
import android.widget.CheckedTextView
import android.widget.RelativeLayout
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        //getting our layout
        val relativeLayout = findViewById<RelativeLayout>(
        //using checktextview
        val checkedTextView = CheckedTextView(this)
        checkedTextView.layoutParams = RelativeLayout.
        //using our strings.xml to set text
        //initially the checkbox in unchecked
        checkedTextView.isChecked = false
        //Onclick event for checkbox
        checkedTextView.setOnClickListener {
            checkedTextView.isChecked = !checkedTextView.isChecked
            checkedTextView.setCheckMarkDrawable(if (checkedTextView.isChecked)
                else android.R.drawable.checkbox_off_background)
            //using our strings.xml setting the starting message of the toast
            val message = getString(R.string.pre_msg) + " "
                    if (checkedTextView.isChecked)
                    else getString(R.string.unchecked)
            Toast.makeText(this@MainActivity, message, Toast.LENGTH_LONG).show()
        // Add Checkbox to RelativeLayout


AndroidManifest.xml file

<?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:

Last Updated : 18 Feb, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads