How to set JetpackCompose Button Background Color

5.8k views Asked by At

I tried the code in background color on Button in Jetpack Compose

Button(
    onClick = {  },
    backgroundColor = Color.Yellow) {
}

but it doesn't recognize backgroundColor anymore.

I tried the below

Button(
    modifier = Modifier.background(Color.Yellow),
    onClick = { }){
}

Doesn't error out but the color is not setting

I'm using 1.0.0-alpha07 of Jetpack Compose. What's the way to set background color of the button?

3

There are 3 answers

0
veritas1 On BEST ANSWER

Try this:

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

You can use the ButtonDefaults.buttonColors using the backgroundColor property:

Something like:

Button(
     onClick = {  },
     colors = ButtonDefaults.buttonColors(
          backgroundColor = Color.Red)
)
0
Shahab Saalami On

In Material3:

It's changed a bit, you have to set containerColor to change background color.

Example:

    Button(
            onClick = { //todo },
            colors = ButtonDefaults.buttonColors(
                containerColor = Color.Yellow, // your background color
                contentColor = Color.Unspecified,// change text color
                disabledContainerColor = Color.Unspecified,
                disabledContentColor = Color.Unspecified
            ) 
        )

source: https://developer.android.com/reference/kotlin/androidx/compose/material3/ButtonDefaults#buttonColors(androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color)