![]() |
VOOZH | about |
CheckedTextView is an extension of TextView in Android that includes a checkmark, making it function like a checkbox. It is commonly used in list views where items can be selected or toggled between checked and unchecked states. Users can tap the text to change its checked status, and the checkmark updates accordingly.
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
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.
activity_main.xml:
Here, we first declare a checkedTextView variable and find the xml checkedTextView by using id.
val checkedTextView: CheckedTextView = findViewById(R.id.checkedTextView)then, check using the conditional statement like
if (checkedTextView.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.
MainActivity.kt:
| Attributes | Description |
|---|---|
| android:id | Gives a unique ID to the Textview. |
| android:gravity | We can align text of the Textview vertically or horizontally or both. |
| android:height | Used to set height of the Textview. |
| android:width | Sets width of the TextView. |
| android:padding | Used to set padding. |
| android:checkMark | Used to set the drawable for checkmark. |
| android:checkMarkTint | Used to set tint to the check mark. |
| android:checkMarkTintMode | Blending mode used to apply the check mark tint. |
| android:checked | Used to set the initial checked state of the checkedTextView which is false by default. |