background color on Button in Jetpack Compose

Viewed 34559
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().

4 Answers

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)
) {
   /**/
}

You can use the ButtonDefaults.buttonColors

Button(
     onClick = {  },
     colors = ButtonDefaults.buttonColors(
          backgroundColor = Color.White,
          contentColor = Color.Red)
)

The ButtonConstants.defaultButtonColor is Deprecated at 1.0.0-alpha09 use :

 colors = ButtonDefaults.buttonColors(backgroundColor = Color.Yellow)

Compose background buttons color create a variable mainButtonColor and define background Color and content Color

implementation 'androidx.compose.material3:material3:1.0.0-alpha02'
    val mainButtonColor = ButtonDefaults.buttonColors(
        containerColor = androidx.compose.ui.graphics.Color.Red,
        contentColor = MaterialTheme.colorScheme.surface
    )

    Row {
        Button(colors = mainButtonColor, onClick = {}, modifier = Modifier.padding(8.dp)) {
            Text(text = "Custom colors")
        }
    }

Change button color

Related