Android Checkbox 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. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

Hello Readers! In this tutorial, we are going to learn about android checkbox using kotlin with example. We will also go through different attributes of checkbox that are used to customise checkbox widget.

Output

Tutorialwing Android Kotlin checkbox Output Android Checkbox using kotlin Android Checkbox Widget in kotlin

Tutorialwing Android Kotlin checkbox Output

Getting Started

Android checkbox widget can be defined as below –

A checkbox is a specific type of two-states button that can be either checked or unchecked.

Different Attributes of Android Checkbox Widget

Attributes of checkbox are inherited from Textview, Compound Button and View. Some of the attributes inherited from Textview are –

Sr. XML Attributes Description
1 android:ems Makes view exactly this ems wide.
2 android:gravity It specifies how the text inside the view be aligned. E.g. right, left ,center etc.
3 android:height Sets height of the view.
4 android:maxWidth Sets maximum allowed width of the view.
5 android:minWidth Sets minimum allowed width of the view.
6 android:width Sets width of the view

Attributes of Checkbox inherited from Compound Button are –

Sr. XML Attributes Description
1 android:button Sets drawable for button graphic.
2 android:buttonTint Sets tint to button graphic.
3 android:buttonTintMode Blending mode used to apply the button graphic tint.

Attributes of Checkbox inherited from View are –

Sr. XML Attributes Description
1 android:id It sets unique identifier for view.
2 android:padding Sets padding of view.
3 android:onClick Defines the operations to perform when this view is clicked
4 android:visibility Sets the visibility (visible, gone etc.) of the Checkbox.
5 android:tooltipText Text to be shown in popup when cursor is hovered on view.
6 android:background Sets background to view.
7 android:alpha Sets alpha in view.



Example of Android Checkbox Using Kotlin

At first, we will create an android application. Then, we will use checkbox widget in kotlin file.

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

Since, we have a new project. Now, we will modify the xml and kotlin file to use checkbox using kotlin.

2. Modify values folder

<resources>
	<string name="app_name">CheckboxUsingKotlin</string>
	<string name="check_it">Check it</string>
</resources>

Other value folder have not been changed. So, we are not going to mention it here.

3. Use Checkbox Widget in xml file

Open res/layout/activity_main.xml file. Then, add code for checkbox in 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">

	<CheckBox
		android:id="@+id/checkBox"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:padding="10dp"
		android:text="@string/check_it"/>

</LinearLayout>

Note that we have used Checkbox widget in xml file. Now, we will access this widget in kotlin file.

4. Access Checkbox Widget in Kotlin file

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

package com.tutorialwing.checkbox

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

class MainActivity : AppCompatActivity() {

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

        val checkBox = findViewById<CheckBox>(R.id.checkBox)
        checkBox?.setOnCheckedChangeListener { buttonView, isChecked ->
            val msg = "You have " + (if (isChecked) "checked" else "unchecked") + " this Check it Checkbox."
            Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show()
        }
    }
}

In this file, we have accessed checkbox using it’s id. Then, we have added a listener to show toast message when checked/unchecked of checkbox is changed.

Since AndroidManifest.xml file is very important in any android project. We are also going to mention it here.

AndroidManifest.xml file

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

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.checkbox"
		  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 Checkbox 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