Skip to content

Latest commit

 

History

History
69 lines (53 loc) · 1.62 KB

checkbox.md

File metadata and controls

69 lines (53 loc) · 1.62 KB
title
Android

Overview

Checkbox component is provided in two flavors, as a simple checkbox and as a row with an additional label and a description:

Usage

Checkbox accepts a Boolean state and a callback on its change.

package test

import androidx.compose.runtime.Composable

@Composable
fun Example() {
    Checkbox(
        checked = false,
        onCheckedChange = {},
    )
}

CheckboxField has two additional slots - description and label.

@Composable
fun Example() {
    CheckboxField(
        checked = false,
        onCheckedChange = {},
        description = { Text("If selected, you will get the latest updates.") }
    ) {
        Text("Autoupdate")
    }
}

UI testing

The slotting API allows you to add a testTag to particular component parts. Utilize assertIsOn and assertIsOff to check the state.

composeTestRule.setContent {
    var checked by remember { mutableStateOf(false) }
    CheckboxField(
        modifier = Modifier.testTag("checkbox"),
        checked = checked,
        onCheckedChange = { checked = !checked },
    ) {
        Text("Label")
    }
}

val checkbox = composeTestRule.onNodeWithTag("checkbox")
checkbox.assertIsOff()
checkbox.performClick()
checkbox.assertIsOn()

Customization

Checkbox and CheckboxField appearance is not customizable.