How to create a ColorDrawable from Jetpack Compose Color?

Viewed 57

Is there a way to convert jetpack Compose's androidx.compose.ui.graphics.Color to android.graphics.drawable.ColorDrawable ?

I tried ColorDrawable(Color.Red.toArgb()) but its not working!

1 Answers

You tried it in the correct way.

Here is a sample that does work

@Composable
fun MyComposable() {
    val color = androidx.compose.ui.graphics.Color.Red

    AndroidView(
        modifier = Modifier
            .fillMaxWidth()
            .height(20.dp),
        factory = { context ->
            View(context).apply {
                background = ColorDrawable(color.toArgb())  
            }
        }
    )    
}
Related