Dynamic CheckedTextView in Kotlin - GeeksforGeeks

18 Feb, 2021
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:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    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>
The final step is to code our CheckedTextView. Open app/src/main/java/yourPackageName/MainActivity.kt
package com.geeksforgeeks.myfirstkotlinapp  import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.view.ViewGroupimport android.widget.CheckedTextViewimport android.widget.RelativeLayoutimport 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)      }}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    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>
Android-View
Kotlin Android
Picked
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Resource Raw Folder in Android Studio
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
Retrofit with Kotlin Coroutine in Android
How to Post Data to API using Retrofit in Android?
Android UI Layouts
Kotlin Array
Retrofit with Kotlin Coroutine in Android
How to Get Current Location in Android?
Kotlin Setters and Getters