Open In App

Dynamic CheckedTextView in Kotlin

Improve
Improve
Like Article
Like
Save
Share
Report

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"?>
<RelativeLayout
    android:id="@+id/relativeLayout"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
</RelativeLayout>


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.




<resources>
    <string name="app_name">DynamicCheckedTextView</string>
    <string name="checked">checked</string>
    <string name="unchecked">unchecked</string>
    <string name="pre_msg">TextView is</string>
</resources>


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 androidx.appcompat.app.AppCompatActivity
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?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //getting our layout
        val relativeLayout = findViewById<RelativeLayout>(R.id.relativeLayout)
        //using checktextview
        val checkedTextView = CheckedTextView(this)
        checkedTextView.layoutParams = RelativeLayout.
            LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        //using our strings.xml to set text
        checkedTextView.setText(R.string.app_name)
        //initially the checkbox in unchecked
        checkedTextView.isChecked = false
        checkedTextView.setCheckMarkDrawable(android.R.drawable.
            checkbox_off_background)
  
        //Onclick event for checkbox
        checkedTextView.setOnClickListener {
            checkedTextView.isChecked = !checkedTextView.isChecked
            checkedTextView.setCheckMarkDrawable(if (checkedTextView.isChecked)
                android.R.drawable.checkbox_on_background 
                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)
                getString(R.string.checked) 
                    else getString(R.string.unchecked)
            Toast.makeText(this@MainActivity, message, Toast.LENGTH_LONG).show()
        }
  
        // Add Checkbox to RelativeLayout
        relativeLayout?.addView(checkedTextView)
  
    }
}


AndroidManifest.xml file




<?xml version="1.0" encoding="utf-8"?>
    package="com.geeksforgeeks.myfirstkotlinapp">
  
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
  
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
  
</manifest>


Run as Emulator:



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