Theory
Pick as many as you like
A FestConnect student might attend Robotics and Dance and Coding, any combination. You need a control where several options can be selected at once, independently.
That is the CheckBox: a small tickable box, one per option, each on or off by itself. It shares the checked model you met on the ToggleButton, but the key idea here is independence: ticking one does nothing to the others, which is exactly what 'pick any number' needs.
Theory
A shopping list with tick boxes
A shopping list has a little box beside each item; you tick every item you want, and ticking 'milk' has no effect on 'bread'. You might tick none, some, or all. CheckBoxes are that list on screen: each box is its own decision. Compare a RadioButton group (next lesson), which is more like 'choose one size', where picking Large un-picks Medium.
Theory
Read each box independently
Each CheckBox is separate, with the familiar checked methods:
- `isChecked()`: is this box ticked? (boolean)
- `setChecked(true/false)`: tick or untick from code.
- `setOnCheckedChangeListener(...)`: react when it changes.
To collect a multi-choice answer, you check each box's isChecked() on its own. A default CheckBox shows the standard tick; a custom one swaps the indicator via android:button pointing at a drawable/selector.
Practical
FestConnect: pick your events
// XML: three CheckBoxes with ids robotics, dance, coding
CheckBox robotics = findViewById(R.id.robotics);
CheckBox dance = findViewById(R.id.dance);
CheckBox coding = findViewById(R.id.coding);
register.setOnClickListener(v -> {
StringBuilder chosen = new StringBuilder();
if (robotics.isChecked()) chosen.append("Robotics ");
if (dance.isChecked()) chosen.append("Dance ");
if (coding.isChecked()) chosen.append("Coding ");
Toast.makeText(this, "Events: " + chosen, Toast.LENGTH_LONG).show();
});
// each box is independent: ticking dance does NOT untick roboticsThink first
Why three separate checks?
The code calls isChecked() on robotics, dance, and coding separately. Why not one check for 'the selected event'? What does that tell you about how CheckBoxes differ from a radio group?
Show the answer
Because a student can select several events at once, there is no single 'the selected event', each box is an independent yes/no, so you must ask each one. That independence is exactly what separates CheckBoxes from a RadioButton group: a radio group represents one choice among many (so you ask 'which one is selected?'), while CheckBoxes represent many independent choices (so you ask each 'are you ticked?'). Multi vs single is the whole distinction.
Quiz
When should you use CheckBoxes rather than a RadioButton group?
- When the user may select more than one option at the same time
- When the user must pick exactly one option
- When you need a button that fires once and forgets
- When you want to show a brief auto-dismissing message
Show the answer
When the user may select more than one option at the same time
CheckBoxes are for multi-select: the user can tick any number of independent options (A). Picking exactly one is the job of a RadioButton group (B). A fire-and-forget action is a plain Button (C), and a brief auto-dismissing message is a Toast (D). Remember: CheckBox = many, RadioButton = one. That choice drives which widget you place.
Watch out
CheckBox traps
1. Using CheckBoxes when only one choice is valid: if the options are mutually exclusive (a single size, a single year), use a RadioButton group, not CheckBoxes.
2. Testing the label instead of the state: use isChecked() for the boolean, not the text.
3. Forgetting each box is independent: there is no automatic 'unselect the others'; that behaviour only comes from a RadioGroup.
Theory
Next: pick exactly one
For the opposite need, choosing a single year (First/Second/Third), CheckBoxes are wrong because a student is in one year only. The next lesson is the RadioButton, grouped in a RadioGroup so selecting one automatically clears the rest. It reuses isChecked() but adds the single-choice grouping you saw hinted here.
Summary
Key takeaways
- A CheckBox is an independently tickable on/off box, used when the user may pick several options.
- Read with isChecked() (boolean), set with setChecked(), react with setOnCheckedChangeListener.
- Collect a multi-choice answer by checking each box's isChecked() independently.
- Ticking one CheckBox does not affect the others (no automatic exclusion).
- Use CheckBoxes for many choices; use a RadioButton group for exactly one.
- Memory hook: a shopping list with independent tick boxes.