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 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:
Click on File, then New => New ProjectThen, check Include Kotlin Support and click next button.Select minimum SDK, whatever you need.Select Empty activity and then click finish.
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.
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"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"    android:orientation="vertical">      <CheckedTextView        android:id="@+id/ctv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:gravity="center"        android:text="@string/checkedTextView"/>  </LinearLayout>
Make some changes in strings.xml file like app_name and other strings used in Kotlin file.
<resources>    <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></resources>
Here, we first declare a checkedTextView variable and find the xml checkedTextView by using id.
val CTView = findViewById(R.id.ctv)
then, check using the conditional statement like
if (CTView.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.
package com.geeksforgeeks.myfirstkotlinapp  import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.widget.CheckedTextViewimport android.widget.Toast  class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          val CTView = findViewById<CheckedTextView>(R.id.ctv)        if (CTView != null) {            CTView.isChecked = false            CTView.setCheckMarkDrawable(                android.R.drawable.checkbox_off_background)              CTView.setOnClickListener {                CTView.isChecked = !CTView.isChecked                CTView.setCheckMarkDrawable(                    if (CTView.isChecked)                        android.R.drawable.checkbox_on_background                    else                        android.R.drawable.checkbox_off_background)                  val msg = getString(R.string.msg_shown)+ " " +                        getString(if (CTView.isChecked)                            R.string.checked else R.string.unchecked)                Toast.makeText(this@MainActivity, msg,                    Toast.LENGTH_SHORT).show()            }        }    }}
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"?><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.
How to Create and Add Data to SQLite Database in Android?
Broadcast Receiver in Android With Example
Resource Raw Folder in Android Studio
Services in Android with Example
Content Providers in Android with Example
Broadcast Receiver in Android With Example
Android UI Layouts
Kotlin Array
Services in Android with Example
Content Providers in Android with Example