Skip to content

Android CheckBox and Example in Kotlin

  • by

Android CheckBox is used for the android application user interface, where the user can select any one or more options. Checkbox example is the food ordering android app, where the user can select multiple food options. Checkbox control has both option select or deselect (checked and unchecked) option. CompoundButton class is the parent class of the CheckBox class.

Android CheckBox and Example in Kotlin (andorid studio)

You can create a Checkbox in your layout. Checkbox options allow to user select multiple items, so checkbox needs to manage a separately. The checkbox is a type of two state button either unchecked or checked in Android.

How to Use Checkbox 

Add the Checkbox in the resource layout file.

<CheckBox
        android:id="@+id/cb_single"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="CheckBox"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

Then set onClicklistener on Checkbox like that

// Single checked box
        cb_single.setOnClickListener(View.OnClickListener {
            if (cb_single.isChecked) {
                message(cb_single.text.toString() + " Checked")
            } else {
                message(cb_single.text.toString() + " UnChecked")
            }
        })

Let’s Build a Complete Example of Android Checkbox :

How to When (switch) condition do with kotlin android: for example if you have multiple checkboxes? Let check this example with a simple one and multiple checkboxes in an app.

Step 1. Create new project “Build Your First Android App in Kotlin
Step 2. Add below code in “activity_main.xml” resource file

Here  5 checkbox is used, where 4 checkboxes have performed an event, added the android:onClick attribute. And 1 will be use OnClickListener.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:text="Select your favorite shoes brands"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/cb_addidas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:onClick="onCheckboxClicked"
        android:text="Adidas"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <CheckBox
        android:id="@+id/cb_nike"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:onClick="onCheckboxClicked"
        android:text="Nike"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cb_addidas" />

    <CheckBox
        android:id="@+id/cb_reebok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:onClick="onCheckboxClicked"
        android:text="Reebok"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cb_nike" />

    <CheckBox
        android:id="@+id/cb_converse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:onClick="onCheckboxClicked"
        android:text="Converse"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cb_reebok" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="Single checkbox example"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cb_converse" />

    <CheckBox
        android:id="@+id/cb_single"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="CheckBox"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

</android.support.constraint.ConstraintLayout>
Step 3. Open the “MainActivity.kt” and add the following code 
package `in`.eyehunt.androidcheckbox

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.CheckBox
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

        // Single checked box
        cb_single.setOnClickListener(View.OnClickListener {
            if (cb_single.isChecked) {
                message(cb_single.text.toString() + " Checked")
            } else {
                message(cb_single.text.toString() + " UnChecked")
            }
        })
    }

    // multiple checkbox click method
    fun onCheckboxClicked(view: View) {
        var checked = view as CheckBox
        if (cb_addidas == checked) {
            message(cb_addidas.text.toString() + if (cb_addidas.isChecked) " Checked " else " UnChecked ")
        }
        if (cb_nike == checked) {
            message(cb_nike.text.toString() + if (cb_nike.isChecked) " Checked " else " UnChecked ")
        }

        if (cb_converse == checked) {
            message(cb_converse.text.toString() + if (cb_converse.isChecked) " Checked " else " UnChecked ")
        }
        if (cb_reebok == checked) {
            message(cb_reebok.text.toString() + if (cb_reebok.isChecked) " Checked " else " UnChecked ")
        }
    }
    fun message(str: String) {
        Toast.makeText(this, str, Toast.LENGTH_LONG).show()
    }
}
Step 4. Now Run the application, in an emulator or On your Android device

Output screenshot Android Checkbox example :

Android CheckBox and Example in Kotlin kotlin

Download source code Android CheckBox in kotlin

https://github.com/EyeHunts/AndroidCheckBox

Do comment if you have any doubt and suggestion on this tutorial.

Note: This example (Project) is developed in Android Studio 3.1.3. Tested on Android 9 ( Android-P), compile SDK version API 27: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”27″

Coding in Kotlin

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading