Checkbox- AGN HUB

 CheckBox in Kotlin (Android)

The CheckBox is a two-state button that can be either checked or unchecked. It is commonly used for selecting multiple options from a set.


Key Properties

  1. isChecked: Boolean value indicating whether the CheckBox is checked or unchecked.

  2. setOnCheckedChangeListener: Listener to handle the change in the checked state.

  3. text: Text displayed next to the CheckBox.


Basic Implementation

XML Layout

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:padding="16dp">


    <CheckBox

        android:id="@+id/checkBox"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Option 1" />


    <CheckBox

        android:id="@+id/checkBox2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Option 2" />


    <Button

        android:id="@+id/buttonSubmit"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Submit"

        android:layout_marginTop="16dp" />

</LinearLayout>

Kotlin Code

import android.os.Bundle

import android.widget.Button

import android.widget.CheckBox

import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)


        val checkBox1: CheckBox = findViewById(R.id.checkBox)

        val checkBox2: CheckBox = findViewById(R.id.checkBox2)

        val buttonSubmit: Button = findViewById(R.id.buttonSubmit)


        // Set OnClickListener for the button

        buttonSubmit.setOnClickListener {

            val message = StringBuilder("Selected Options: ")


            if (checkBox1.isChecked) {

                message.append("Option 1 ")

            }


            if (checkBox2.isChecked) {

                message.append("Option 2 ")

            }


            if (!checkBox1.isChecked && !checkBox2.isChecked) {

                message.append("None")

            }


            Toast.makeText(this, message.toString(), Toast.LENGTH_SHORT).show()

        }

    }

}


Handling CheckBox State

Check if a CheckBox is Checked:

if (checkBox.isChecked) {

    // Do something when checked

}

Programmatically Set the Checked State:

checkBox.isChecked = true // Check the box

checkBox.isChecked = false // Uncheck the box

Add a Change Listener:

checkBox.setOnCheckedChangeListener { buttonView, isChecked ->

    if (isChecked) {

        Toast.makeText(this, "${buttonView.text} is checked", Toast.LENGTH_SHORT).show()

    } else {

        Toast.makeText(this, "${buttonView.text} is unchecked", Toast.LENGTH_SHORT).show()

    }

}


Styling and Customization

  1. Customize Text Color:

  2. <CheckBox

  3.     android:id="@+id/checkBox"

  4.     android:layout_width="wrap_content"

  5.     android:layout_height="wrap_content"

  6.     android:text="Custom Option"

  7.     android:textColor="@color/black" />

  8. Set Drawable for Check Mark:

  9. <CheckBox

  10.     android:id="@+id/checkBox"

  11.     android:layout_width="wrap_content"

  12.     android:layout_height="wrap_content"

  13.     android:button="@drawable/custom_checkbox_drawable" />


Example with Multiple CheckBoxes

XML Layout

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:padding="16dp">


    <CheckBox

        android:id="@+id/checkBox1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Pizza" />


    <CheckBox

        android:id="@+id/checkBox2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Burger" />


    <CheckBox

        android:id="@+id/checkBox3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Pasta" />


    <Button

        android:id="@+id/buttonSubmit"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Show Selected"

        android:layout_marginTop="16dp" />

</LinearLayout>

Kotlin Code

buttonSubmit.setOnClickListener {

    val selectedItems = mutableListOf<String>()


    if (checkBox1.isChecked) selectedItems.add("Pizza")

    if (checkBox2.isChecked) selectedItems.add("Burger")

    if (checkBox3.isChecked) selectedItems.add("Pasta")


    val message = if (selectedItems.isNotEmpty()) {

        "You selected: ${selectedItems.joinToString(", ")}"

    } else {

        "No options selected"

    }


    Toast.makeText(this, message, Toast.LENGTH_SHORT).show()

}


Common Use Cases

  1. Multi-Select Forms: E.g., survey forms or preference settings.

  2. Filters: E.g., filtering a product list by categories.

  3. Feature Toggles: Enable/disable specific app features.

Comments

Popular posts from this blog

RadioButtons-AGN HUB

Timepicker- AGN HUB