Ultimate Guide to Material 3 Checkboxes in Android XML (2025 Tutorial + Examples)

Ultimate Guide to Material 3 Checkboxes in Android: Tutorial & Examples for 2025 🚀

Overview 📖

Material 3 Checkboxes allow users to select one or more options from a list, toggle settings, or manage sub-selections. This guide covers their implementation, including checked, unchecked, indeterminate, and error states, along with theming for 2025. ✅

Overview of Material 3 Checkboxes

Design and API Documentation 🔗

Using Checkboxes 🤖

Add the Material Components for Android library dependency to use Material 3 Checkboxes. See the Get Started guide. Checkboxes support checked, unchecked, indeterminate, and error states, with accessibility built-in. Use a Theme.Material3.* theme for auto-inflation as com.google.android.material.checkbox.MaterialCheckBox. 🛠️

Making Checkboxes Accessible ♿

Checkboxes are readable by screen readers like TalkBack, with text labels automatically provided. Set android:contentDescription for custom accessibility labels if needed. See the Android accessibility guide. 🗣️

Behavior and States 🎬

Toggle states with isChecked or checkedState, set error with errorShown, or listen for changes with setOnCheckedChangeListener. Example:

      
checkbox.isChecked = true
checkbox.errorShown = true
checkbox.setOnCheckedChangeListener { _, isChecked ->
    // Handle check change
}
      
      
    

Checkbox Anatomy 🌟

Material 3 Checkboxes consist of a container (square button) and an icon (checkmark or dash), with optional text labels. They support checked, unchecked, indeterminate, and error states. 📋

Checkbox anatomy diagram
  • Container: Rounded square, holds the icon.
  • Icon: Checkmark (checked), dash (indeterminate), or empty (unchecked). ✅
  • Label: Optional text describing the option.

Checkbox Example 🔲

Checkboxes allow multiple selections, with parent-child relationships for hierarchical lists. 📝

Checkbox with parent-child hierarchy

Checkbox Example 💻

API and source code:

The following example shows a parent checkbox with four child checkboxes, updating states dynamically.

      

    
    
        
        
        
        
    

      
      
    
      
private var isUpdatingChildren = false
val parent = findViewById(R.id.checkbox_parent)
val children = listOf(
    findViewById(R.id.checkbox_child_1),
    findViewById(R.id.checkbox_child_2),
    findViewById(R.id.checkbox_child_3),
    findViewById(R.id.checkbox_child_4)
)
val parentListener = OnCheckedStateChangedListener { _, state ->
    if (state != MaterialCheckBox.STATE_INDETERMINATE) {
        isUpdatingChildren = true
        children.forEach { it.isChecked = parent.isChecked }
        isUpdatingChildren = false
    }
}
parent.addOnCheckedStateChangedListener(parentListener)
val childListener = OnCheckedStateChangedListener { _, _ ->
    if (!isUpdatingChildren) {
        val checkedCount = children.count { it.isChecked }
        parent.removeOnCheckedStateChangedListener(parentListener)
        parent.checkedState = when {
            checkedCount == children.size -> MaterialCheckBox.STATE_CHECKED
            checkedCount == 0 -> MaterialCheckBox.STATE_UNCHECKED
            else -> MaterialCheckBox.STATE_INDETERMINATE
        }
        parent.addOnCheckedStateChangedListener(parentListener)
    }
}
children.forEach { it.addOnCheckedStateChangedListener(childListener) }
children[0].isChecked = true
      
      
    

Anatomy and Key Properties 📏

A checkbox consists of a container and an icon, with an optional label. ✅

Checkbox anatomy diagram
  • Container: Rounded square, holds icon.
  • Icon: Checkmark or dash, indicates state. 📍
  • Label: Descriptive text, optional. ✍️

Attributes 📋

Element Attribute Related method(s) Default value
Container app:buttonTint setButtonTintList, getButtonTintList ?attr/colorOnSurface (states)
app:buttonIcon setButtonIconDrawable, getButtonIconDrawable @mtrl_checkbox_button_icon
Icon app:buttonIconTint setButtonIconTintList, getButtonIconTintList ?attr/colorOnPrimary (states)
Label android:text setText, getText null
State app:checkedState setCheckedState unchecked

Styles 🎨

  • Default style: Widget.Material3.CompoundButton.CheckBox
  • Default style theme attribute: ?attr/checkboxStyle

See the full list of styles and attrs. 🔗

Theming Checkboxes 🖌️

Material Theming customizes checkbox colors and typography. 🎨

Themed Checkboxes with custom colors

Theming Example 💻

Theming example for checkboxes:

      

      
      
    

Specific styles for checkboxes without affecting other components:

      





      
      
    

FAQ ❓

What are Material 3 Checkboxes? 🤔

Controls for selecting multiple options or toggling settings. ✅


What states do they support? 🔢

Checked, unchecked, indeterminate, and error states. 📋


When to use indeterminate state? 🖋️

Use for parent checkboxes when some, but not all, children are checked. 🔄


How to make them accessible? ♿

Labels are auto-provided for TalkBack; add custom contentDescription if needed. 🗣️


Can I customize appearance? 🎨

Yes, theme colors and typography via res/values/styles.xml. 🖌️


How to add the Material 3 library? 📦

Include the Material Components for Android library. See the Get Started guide. 🛠️


Are there updates for 2025? 🗓️

This guide reflects 2025 Material 3 standards for checkboxes. 🚀

Comments

Popular posts from this blog

Creating Beautiful Card UI in Flutter

Master Web Development with Web School Offline

Jetpack Compose - Card View