androidandroid-jetpack-composeandroid-buttonandroid-compose-buttonandroid-jetpack-compose-button

background color on Button in Jetpack Compose


Button(backgroundColor = Color.Yellow) {
    Row {
        Image(asset = image)
        Spacer(4.dp)
        Text("Button")
    }
}

I can not figure out why I can't use background color on Button.

I followed the Compose Layout codelabs.
There is a problem in backgroundColor and asset in Image().


Solution

  • Use containerColor in place of backgroundColor when using Material 3

    Button(
       onClick = {},
       colors = ButtonDefaults.buttonColors(containerColor = Color.Yellow)
    ) {
       /**/
    }
    

    Use ButtonDefaults which is available in 1.0.0-alpha09 to alpha11

    Button(
       onClick = {},
       colors = ButtonDefaults.buttonColors(backgroundColor = Color.Yellow)
    ) {
       /**/
    }
    

    OLD VERSION

    The backgroundColor for Button no longer work in 1.0.0-alpha7

    Use the below instead

    Button(
       onClick = {},
       colors = ButtonConstants.defaultButtonColors(backgroundColor = Color.Yellow)
    ) {
       /**/
    }