Search code examples
colorsandroid-jetpack-compose

How to create a ColorDrawable from Jetpack Compose Color?


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!


Solution

  • 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())  
                }
            }
        )    
    }