CheckBox


CheckBox

1. Definition

A CheckBox is a UI component that allows users to select or deselect one or more options.

It works like a small square box that can be checked (✓) or unchecked.

Example:

☐ Pizza
☐ Burger
☐ Sandwich

After selection:

☑ Pizza
☐ Burger
☑ Sandwich

Simple meaning:

CheckBox = A selectable box used to choose options.

2. Purpose

CheckBox is used when users can select multiple choices.

Common uses:

App FeatureExample
Food ordering appSelect toppings
Settings screenEnable notifications
Survey formSelect hobbies
Terms & conditionsAccept agreement

3. Basic Syntax (XML)

Example layout:

<CheckBox
    android:id="@+id/checkPizza"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pizza"/>

Explanation:

  • id → unique identifier

  • layout_width → width

  • layout_height → height

  • text → label shown beside checkbox

Result:

☐ Pizza

4. Using CheckBox in Kotlin

Example inside Activity:

import android.os.Bundle
import android.widget.CheckBox
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

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

        val pizzaCheck = findViewById<CheckBox>(R.id.checkPizza)

        if (pizzaCheck.isChecked) {
            println("Pizza selected")
        }
    }
}

Explanation:

  • findViewById() connects Kotlin to XML

  • isChecked checks whether the box is selected


5. Important Properties

1. text

Sets label text.

android:text="Burger"

2. checked

Sets default selection.

android:checked="true"

Example result:

☑ Burger

3. textColor

Changes label color.

android:textColor="#000000"

4. textSize

Changes text size.

android:textSize="18sp"

5. padding

Adds space inside.

android:padding="8dp"

6. Important Methods (Kotlin)

isChecked

Checks whether checkbox is selected.

if (checkBox.isChecked) {
    println("Selected")
}

setChecked()

Set checkbox state.

checkBox.isChecked = true

toggle()

Switch checked/unchecked.

checkBox.toggle()

setOnCheckedChangeListener()

Detect when user checks/unchecks.

checkBox.setOnCheckedChangeListener { _, isChecked ->
    if (isChecked) {
        println("Checked")
    } else {
        println("Unchecked")
    }
}

7. Real-Time Examples

Example 1: Food Order App

☐ Pizza
☐ Burger
☐ Pasta

Users can choose multiple items.


Example 2: App Settings

☑ Enable Notifications
☐ Dark Mode
☐ Auto Update

Example 3: Terms and Conditions

☐ I agree to Terms and Conditions

Button becomes enabled only after checking.

Kotlin example:

val termsCheck = findViewById<CheckBox>(R.id.checkTerms)

termsCheck.setOnCheckedChangeListener { _, isChecked ->
    button.isEnabled = isChecked
}

Example 4: Survey Form

Hobbies:

☐ Reading
☐ Gaming
☐ Sports
☐ Music

Multiple answers allowed.


8. Advantages

✔ Allows multiple selections
✔ Easy for users to understand
✔ Simple to implement
✔ Common in forms and settings


9. Disadvantages

❌ Not suitable when only one option must be selected
(Use **RadioButton instead.)


10. One-Line Summary

CheckBox is an Android UI component that allows users to select or deselect multiple options.



Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form