Quick and easy custom Jetpack compose checkbox

Introduction

In the code below you will see how we can recreate the functionality of a Jetpack Compose Checkbox by using a Box with some animation.

UI Example

UI example can be found here on Reddit

(Reddit embeds are not …


This content originally appeared on DEV Community and was authored by Tristan Elliott

Introduction

  • In the code below you will see how we can recreate the functionality of a Jetpack Compose Checkbox by using a Box with some animation.

UI Example

(Reddit embeds are not working on dev.to)

The code:

@Composable
fun CustomCheckBox(){
    var checked by remember { mutableStateOf(false) }
    val onPrimaryColor = if(checked) MaterialTheme.colorScheme.secondary else  MaterialTheme.colorScheme.onPrimary
    val primaryColor = MaterialTheme.colorScheme.primary
    val animatedColor by animateColorAsState(
        if (checked) MaterialTheme.colorScheme.secondary else primaryColor,
        label = "color"
    )

    Box(modifier = Modifier
        .border(1.dp, onPrimaryColor, RoundedCornerShape(5.dp))
        .height(20.dp)
        .width(20.dp)
        .drawBehind {
            val cornerRadius =
                5.dp.toPx() // must match the RoundedCornerShape(5.dp)
            drawRoundRect(
                color = animatedColor,
                cornerRadius = CornerRadius(cornerRadius, cornerRadius)
            )
        }
        .clip(
            RoundedCornerShape(5.dp)
        )
        .clickable {
            checked = !checked
        }
    ){
        Column( modifier = Modifier.align(Alignment.Center),) {
            AnimatedVisibility(
                checked,
                enter = scaleIn(initialScale = 0.5f), // Scale in animation
                exit = shrinkOut(shrinkTowards = Alignment.Center)
            ) {
                Icon(painter = painterResource(R.drawable.baseline_check_24),
                    contentDescription = "checked",
                    tint = MaterialTheme.colorScheme.primary
                )
            }
        }

    }
}

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.


This content originally appeared on DEV Community and was authored by Tristan Elliott


Print Share Comment Cite Upload Translate Updates
APA

Tristan Elliott | Sciencx (2024-09-10T23:11:05+00:00) Quick and easy custom Jetpack compose checkbox. Retrieved from https://www.scien.cx/2024/09/10/quick-and-easy-custom-jetpack-compose-checkbox/

MLA
" » Quick and easy custom Jetpack compose checkbox." Tristan Elliott | Sciencx - Tuesday September 10, 2024, https://www.scien.cx/2024/09/10/quick-and-easy-custom-jetpack-compose-checkbox/
HARVARD
Tristan Elliott | Sciencx Tuesday September 10, 2024 » Quick and easy custom Jetpack compose checkbox., viewed ,<https://www.scien.cx/2024/09/10/quick-and-easy-custom-jetpack-compose-checkbox/>
VANCOUVER
Tristan Elliott | Sciencx - » Quick and easy custom Jetpack compose checkbox. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/10/quick-and-easy-custom-jetpack-compose-checkbox/
CHICAGO
" » Quick and easy custom Jetpack compose checkbox." Tristan Elliott | Sciencx - Accessed . https://www.scien.cx/2024/09/10/quick-and-easy-custom-jetpack-compose-checkbox/
IEEE
" » Quick and easy custom Jetpack compose checkbox." Tristan Elliott | Sciencx [Online]. Available: https://www.scien.cx/2024/09/10/quick-and-easy-custom-jetpack-compose-checkbox/. [Accessed: ]
rf:citation
» Quick and easy custom Jetpack compose checkbox | Tristan Elliott | Sciencx | https://www.scien.cx/2024/09/10/quick-and-easy-custom-jetpack-compose-checkbox/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.