Android CheckedTextView Tutorial With Example

Hello readers! In this post, we are going to learn how to use android checkedTextView widget in any android application. We will also learn about different attributes that can be used to customise checkedTextView widget.

Output

Tutorialwing Android CheckedTextView Output Android CheckedTextview widget programmatically in android

Tutorialwing Android CheckedTextView Output

Getting Started

CheckedTextView can be defined as below –

CheckedTextView is an extension of TextView that have checkable interface and displays.

Generally, it is used in ListView where setChoiceMode has been set to something other than CHOICE_MODE_NONE.

Attributes of Android CheckedTextView Widget

Some of the popular attributes of android CheckedTextView widget are –

Sr. XML Attributes Description
1 android:checkMark Drawable used for check mark graphic
2 android:checkMarkTint Tint to apply to the check mark.
3 android:checkMarkTintMode Blending mode used to apply the check mark tint.
4 android:checked Indicates the initial checked state of this text.

Attributes of Android CheckedTextView are also inherited from TextView and View. Some of the popular attributes of this widget inherited from TextView are –

Sr. XML Attributes Description
1 android:capitalize If set, it automatically capitalise all the text in the view.
2 android:cursorVisible It specifies whether cursor should be visible or invisible.
3 android:ellipsize It ellipsizes (ends with dots) text if it is too long
4 android:gravity It specifies how text should be aligned within the view. For example, CENTER, VERTICAL_CENTER or HORITOZAL_CENTER etc.
5 android:height It specifies the height of the view.
6 android:width It specifies the width of the view.

Some of the popular attributes of Android CheckedTextView inherited from View are –

Sr. XML Attributes Description
1 android:alpha It sets the alpha property of the view. Values lies between 0 and 1.
2 android:background It sets the background of the view.
3 android:clickable It specifies whether view is clickable or not.
4 android:elevation It sets base z depth of the view.
5 android:id It specifies the unique id of the view.
6 android:padding It sets padding of the view.



Example of Android CheckedTextView Widget

At first, we will create an android application. Then, we will use checkedTextView widget in the application.

1. Creating New Project

Follow the steps below to create new project. Please ignore the steps if you have already created a new project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as CheckedTextView. Then, click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use checkedTextView in the project.

2. Modify Values folder

Open res/values/strings.xml file. Then, add below code into it.

<resources>
	<string name="app_name">CheckedTextView</string>
	<string name="pre_msg">CheckedTextView is</string>
	<string name="checked">checked</string>
	<string name="unchecked">unchecked</string>
	<string name="checkedTextView">CheckedTextView</string>
</resources>

3. Use CheckedTextView Widget in xml file

Open res/layout/activity_main.xml file. Then, add below code into it.

<?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"
	android:gravity="center">

	<CheckedTextView
		android:id="@+id/checkedTextView"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:checked="true"
		android:gravity="center"
		android:text="@string/checkedTextView"/>

</LinearLayout>

In activity_main.xml file, we have added a CheckedTextView. Now, we will access this widget in java file.

4. Access CheckedTextView Widget in java file

Open src/main/java/com.tutorialwing.checkedtextview/MainActivity.java file. Then, add below code into it.

package com.tutorialwing.checkedtextview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final CheckedTextView checkedTextView = findViewById(R.id.checkedTextView);
		if (checkedTextView != null) {
			checkedTextView.setChecked(false);
			checkedTextView.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);

			checkedTextView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					checkedTextView.setChecked(!checkedTextView.isChecked());
					checkedTextView.setCheckMarkDrawable(checkedTextView.isChecked() ? android.R.drawable.checkbox_on_background : android.R.drawable.checkbox_off_background);

					String msg = getString(R.string.pre_msg) + " " + (checkedTextView.isChecked() ? getString(R.string.checked) : getString(R.string.unchecked));
					Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
				}
			});
		}
	}
}

In MainActivity.java file, we have accessed CheckedTextView widget defined in xml file. Then, we have set a listener to show toast message when checkedTextView is clicked.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml

Code inside src/main/AndroidManifest.xml file is as below –

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.checkedtextview"
		  xmlns:android="http://schemas.android.com/apk/res/android">

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

When you run the application, you will get output as shown below.

That’s the end of tutorial on Android CheckedTextView.

Leave a Reply