Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 897
shotmeinthehead Avatar asked Oct 15 '20 16:10

shotmeinthehead


People also ask

How do you change the background of a button?

To change the background color of the button, use the CSS background-color property and give it a value of a color of your taste. In the . button selector, you use background-color:#0a0a23; to change the background color of the button.


Video Answer


2 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)
) {
   /**/
}
like image 114
Elye Avatar answered Oct 04 '22 12:10

Elye


You can use the ButtonDefaults.buttonColors

Button(
     onClick = {  },
     colors = ButtonDefaults.buttonColors(
          backgroundColor = Color.White,
          contentColor = Color.Red)
)
like image 28
Gabriele Mariotti Avatar answered Oct 04 '22 12:10

Gabriele Mariotti