CheckedTextView in Android | AndroidMonks

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).

YOU ARE YOUR BOSS
I agree to have my personal information transfered to MailChimp ( more information )
Join over 5.000 visitors who are receiving our free content and learn how to program with Android. The growth of Android is estimated to reach a whooping 83% by 2020. Hop on the train and start making money!
We hate spam. Your email address will not be sold or shared with anyone else.

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,

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

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.

CheckedTextView  final Output Screen
CheckedTextView final Output Screen

Drop in any comments you have below.

“Learn and Be Curious”

Also Read  CheckBox in Android - Implementation and Overview

Leave a Comment

Your email address will not be published. Required fields are marked *