Blog Infos
Author
Published
Topics
, , , ,
Author
Published

Header

 

Starting with version 1.4.0-alpha07 of compose.material3:material3 we get a new API for Checkbox, which provides us an option to customize the stroke of the checkmark and checkbox-outline.

 

Existing API

 

@Preview(showBackground = true)
@Composable
fun CheckboxOldSample() {
    val checkedState = remember { mutableStateOf(true) }
    Row(
        modifier = Modifier.fillMaxWidth(),
        verticalAlignment = androidx.compose.ui.Alignment.CenterVertically
    ) {
        Checkbox(
            checked = checkedState.value,
            onCheckedChange = { checkedState.value = it }
        )
        Text(text = "Old Checkbox API"
    }
}

 

Docs of existing API

New API implementation

 

@Composable
fun CheckboxWithRoundedStrokes() {
    val strokeWidthPx = with(LocalDensity.current) { floor(CheckboxDefaults.StrokeWidth.toPx()) }
    val checkmarkStroke =
        remember(strokeWidthPx) {
            Stroke(
                width = strokeWidthPx,
                cap = StrokeCap.Square,
                join = StrokeJoin.Round,
                pathEffect = PathEffect.dashPathEffect(floatArrayOf(2f, 6f))
            )
        }
    val outlineStroke = remember(strokeWidthPx) {
        Stroke(width = 8f)
    }
    val checkedState = remember { mutableStateOf(true) }
    Row(
        modifier = Modifier.fillMaxWidth(),
        verticalAlignment = androidx.compose.ui.Alignment.CenterVertically
    ) {
        Checkbox(
            checked = true,
            onCheckedChange = { },
            // New properties
            checkmarkStroke = checkmarkStroke,
            outlineStroke = outlineStroke
        )

        Text(text = "New Checkbox API")
    }
}

 

Demo

Preview of 2 states — New API

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

,

Compose Beyond Material Design

In the implementation of Jetpack compose at our organization, one of the issues that popped up was how to implement a Design system that is not adapted from material design but true to what our…
Watch Video

Compose Beyond Material Design

Frank Tamre

Compose Beyond Material Design

Frank Tamre

Compose Beyond Material Design

Frank Tamre

Jobs

No results found.

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
In the world of Jetpack Compose, where designing reusable and customizable UI components is…
READ MORE
blog

How to animate BottomSheet content using Jetpack Compose

Early this year I started a new pet project for listening to random radio…
READ MORE
Menu