A CheckBox is a special kind of button in Android which has two states either checked or unchecked. The Checkbox is a very common widget to be used in Android. There are many other uses of the CheckBox widget like offering a list of options to the user to choose from and the options are mutually exclusive i.e., the user can select more than one option. This feature of the CheckBox makes it a better option to be used in designing multiple-choice questions application or survey application in android.
Class hierarchy of CheckBox class in Kotlin
kotlin.Any
↳android.view.View
↳ android.view.TextView
↳ android.widget.Button
↳ android.widget.CompoundButton
↳ android.widget.CheckBox
XML attributes of CheckBox widget
XML Attributes | Description |
---|
android:id | Used to uniquely identify a CheckBox |
android:checked | To set the default state of a CheckBox as checked or unchechek |
android:background | To set the background color of a CheckBox |
android:text | Used to store a text inside the CheckBox |
android:fontFamily | To set the font of the text of the CheckBox |
android:textSize | To set the CheckBox text size |
android:layout_width | To set the CheckBox width |
android:layout_height | To set the CheckBox height |
android:gravity | Used to adjust the CheckBox text alignment |
android:padding | Used to adjust the left, right, top and bottom padding of the CheckBox |
Example
This example demonstrates the steps involved in designing an activity that consists of 5 CheckBox and an additional submit button to display a toast message that user response has been recorded.
Note: Following steps are performed on Android Studio version 4.0
Steps Implementation of CheckBox with Example
We will understand the CheckBox Implementation in Android Kotlin with a example. Below is the Step by Step implementation for it.
Step 1: Create new project
- Click on File, then New => New Project.
- Choose “Empty Activity” for the project template.
- Select language as Kotlin.
- Select the minimum SDK(According to the application needs).
Step 2: Open activity_main.xml file
Below is the code for activity_main.xml
file to add 5 CheckBox. A normal "submit" button is also added to display a toast message that user response has been recorded.
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#168BC34A"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Services provided by GeeksforGeeks"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17000002" />
<LinearLayout
android:id="@+id/checkBox_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.18">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Coding contests"
android:textSize="16sp"
android:padding="7dp"/>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Civil Engineering Courses"
android:textSize="16sp"
android:padding="7dp"/>
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Computer Science Courses"
android:textSize="16sp"
android:padding="7dp"/>
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Company specific coding questions"
android:textSize="16sp"
android:padding="7dp"/>
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Download movies"
android:textSize="16sp"
android:padding="7dp"/>
</LinearLayout>
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#AB4CAF50"
android:text="SUBMIT"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox_container"
app:layout_constraintVertical_bias="0.23000002" />
</androidx.constraintlayout.widget.ConstraintLayout>
Layout:
Step 3: Open MainActivity.kt file
Below is the code for MainActivity.kt
file to access CheckBox widget in Kotlin file and show a proper message whenever the submit button is clicked by the user.
MainActivity.kt:
Java
package com.gfg.checkbox_kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Assigning id of the submit button
val button : Button = findViewById(R.id.submitButton)
// Actions to be performed
// when Submit button is clicked
button.setOnClickListener{
// Display toast message
Toast.makeText(applicationContext,"Your response has been recorded"
, Toast.LENGTH_LONG).show()
}
}
}
Output:
Similar Reads
CheckedTextView in Kotlin CheckedTextView is an extension of TextView in Android that includes a checkmark, making it function like a checkbox. It is commonly used in list views where items can be selected or toggled between checked and unchecked states. Users can tap the text to change its checked status, and the checkmark
2 min read
Kotlin Collections In Kotlin, collections are used to store and manipulate groups of objects or data. There are several types of collections available in Kotlin, including:Collection NameDescriptionLists Ordered collections of elements that allow duplicates.Sets Unordered collections of unique elements.Maps Collection
6 min read
Kotlin Inheritance Kotlin supports inheritance, which allows you to define a new class based on an existing class. The existing class is known as the superclass or base class, and the new class is known as the subclass or derived class. The subclass inherits all the properties and functions of the superclass, and can
10 min read
Kotlin functions In Kotlin, functions are used to encapsulate a piece of behavior that can be executed multiple times. Functions can accept input parameters, return values, and provide a way to encapsulate complex logic into reusable blocks of code. Table of ContentWhat are Functions?Example of a FunctionTypes of Fu
7 min read
How to Type Check an Object in Kotlin? Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read
Merge Two Collections in Kotlin Kotlin is a statically typed, general-purpose programming language developed by JetBrains. The company famous for creating world-class IDEs like IntelliJ IDEA, PhpStorm, and AppCode. Kotlin was first introduced by JetBrains in 2011 and is designed to run on the Java Virtual Machine (JVM). It is an o
3 min read