Android CheckedTextview Using Kotlin With Example

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners

Hello Readers! In this post, we are going to learn about how to use android checkedTextView using kotlin in any android application. We will also learn about different attributes of android checkedTextview that can be used to customize it.

Output

Tutorialwing Android CheckedTextView Output Android CheckedTextView Using Kotlin

Tutorialwing Android CheckedTextView Output

Getting Started

Android CheckedTextview can be defined as below –

Android CheckedTextview is a subclass of textView that has checkable interface and displays.

Different Attributes of Android CheckedTextview Widget

Some of the popular attributes of checkedTextview are –

Sr. XML Attributes Description
1 android:checkMark This is used to set the drawable for checkmark (i.e. tick mark in this view)
2 android:checkMarkTint This is used to set tint to the check mark.
3 android:checkMarkTintMode Blending mode used to apply the check mark tint.
4 android:checked It sets the initial checked state of the checkedTextView. By default, it is false.

Some of the popular attributes of android CheckedTextview inherited from Textview are –

Sr. XML Attributes Description
1 android:capitalize Automatically capitalise all the text in the view.
2 android:cursorVisible Specifies whether cursor should be visible or invisible.
3 android:ellipsize If the text are too long, it ellipsises the text instead of showing incomplete text
4 android:gravity Specifies how text should be aligned (CENTER, VERTICAL_CENTER or HORITOZAL_CENTER etc.) within the view.
5 android:height Specifies the height of the view.
6 android:width 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 Sets the alpha property of the view. Values lies between 0 and 1.
2 android:background Sets the background of the view.
3 android:clickable Specifies whether view is clickable or not.
4 android:elevation Sets base z depth of the view.
5 android:id Specifies the unique id of the view.
6 android:padding Sets padding of the view.



Example of Android CheckedTextview Using Kotlin

At first, we will create an android project. Then, we will use android checkedTextview widget in this project.

1. Creating New Project in Kotlin

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

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as CheckedTextview. Then, check Include Kotlin Support and 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. You will get a newly created project successfully if you have followed steps properly.

Now, we will modify xml and kotlin file to use CheckedTextview widget in the application.

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>

No other values folders have been modified. So, we are not going to mention them here.

3. Use Spinner Widget in xml file

Open src/main/res/layout/activity_main.xml file and 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 used CheckedTextview widget in xml file. Now, we will access this android checkedTextview using kotlin file. Then, we will perform some action on it.

4. Access android CheckedTextview using Kotlin file

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

package com.tutorialwing.checkedtextview

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

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val checkedTextView = findViewById<CheckedTextView>(R.id.checkedTextView)
        if (checkedTextView != null) {
            checkedTextView.isChecked = false
            checkedTextView.setCheckMarkDrawable(android.R.drawable.checkbox_off_background)

            checkedTextView.setOnClickListener {
                checkedTextView.isChecked = !checkedTextView.isChecked
                checkedTextView.setCheckMarkDrawable(if (checkedTextView.isChecked) android.R.drawable.checkbox_on_background else android.R.drawable.checkbox_off_background)

                val msg = getString(R.string.pre_msg) + " " + getString(if (checkedTextView.isChecked) R.string.checked else R.string.unchecked)
                Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show()
            }
        }
    }
}

In MainActivity.kt file, we have accessed spinner widget. Then, we have set a listener to show a toast message when CheckedTextview is checked / unchecked.

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

AndroidManifest.xml file

Code inside 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 we run the program, we will get output as shown above.

That’s end of our tutorial on Android CheckedTextview using Kotlin.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners