Contents
CheckedTextView in Android with Example
CheckedTextView is used in order to provide Checkable interface support to TextView. According to the Google Docs, “This is useful when used in a ListView
where the setChoiceMode
has been set to something other thanCHOICE_MODE_NONE
.”
This means that, we can make use of the CheckedTextView inside a ListView without any Choice Mode set. TextView with Choice Mode option creates a effective user experience by showing Checked Images (We will see what i mean by images below).
CheckedTextView – Basics
The CheckedTextView can be created using the CheckedTextView Tag like below. However, it is important to note that, android:checkMark attribute has to be set in order to make it as a CheckedTextView, it will work like a normal TextView otherwise.
Check the below basic implementation of Checked TextView.
<CheckedTextView android:id="@+id/simpleCheckedTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" />
Just like any other View, the default Parameters are layout_width and layout_height. It is important to note that, there is another important attribute which is the android:checkMark attribute, which takes a drawable as its value.
We will see what are the other attributes related to Checked TextView and public methods associated with it.
CheckedTextView – Layout Parameters
Some of the important Layout parameters are,
android:checkMark
– This allows a drawable to be used as the check mark. Can be any image/drawable.android:checkMarkTint
– Tint to apply to the CheckMark.android:checkMarkTintMode
– Allows to blend a tint with the CheckMark used.android:checked
– Indicates the initial checked state of the View.
These are the specific attributes with respect to the Checked TextView. Additionally, you can make use of the attributes which is normally used along with a View. Read more about it here.
CheckedTextView – Public Methods Overview
- getCheckMarkDrawable() – Returns the drawable used by the View.
- getCheckMarkTintList() – Returns the Tint applied to the View, if any.
- getCheckMarkTintMode() – Returns the Mode used with the Tint applied to the View, if any
- isChecked() – Boolean value, tells if the View is Checked or Not
- setCheckMarkDrawable(Drawable d) – From the name it is evident that, this is a setter method, used to set the Drawable to the View.
- setCheckMarkDrawable(int resId) – Sets the check Mark drawable with specific resource ID if present.
- setCheckMarkTintList(ColorStateList tint) – Applies a Tint to the check mark if specified.
- setCheckMarkTintMode(PorterDuff.
Mode tintMode) – Used to apply the tint mode to the View is specified. - setChecked(boolean checked) – Allows the Checked TextView to be either Checked or not
- toggle() – Useful method, allows to reverse the state of the CheckedTextView.
CheckedTextView – Example Application
We have seen, all the properties related to the Checked TextView. Let us jump into creating a example Application below.
First we create a simple Layout, with a Checked TextView. On toggling this textView, we create a callback mechanism to capture the respective action.
checked_textview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckedTextView android:id="@+id/simpleCheckedTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:checked="true" android:gravity="center" android:checkMark="@drawable/tick" android:checkMarkTint="@color/colorPrimary" android:checkMarkTintMode="add" android:text="Simple Checked TextView" /> </LinearLayout>
Time for creating our Activity. This is going to be a simple process, as we will need a callback to capture the onClick and react to it.
We make use of the onClickListener() on the View (CheckedTextView) like below.
Creating the CheckedTextViewClass like below.
package com.example.baradwav.samplemy; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.CheckedTextView; import android.view.View; import android.widget.Toast; public class CheckedTextViewClass extends AppCompatActivity { CheckedTextView checkedTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.checked_textview); checkedTextView = findViewById(R.id.checkedTextview); // Capture on click and toggle the checked TextView checkedTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { checkedTextView.toggle(); if(checkedTextView.isChecked()) { Toast.makeText(view.getContext(),"TextView is Checked",Toast.LENGTH_SHORT).show(); checkedTextView.setCheckMarkDrawable(R.drawable.tick); } else{ Toast.makeText(view.getContext(),"TextView is not Checked",Toast.LENGTH_SHORT).show(); checkedTextView.setCheckMarkDrawable(null); } } }); } }
Once you are done with creating the Activity, its time to check our application. We end up with a application like below.

Drop in any comments you have below.
“Learn and Be Curious”