background color on Button in Jetpack Compose

23,906

Solution 1

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

Solution 2

You can use the ButtonDefaults.buttonColors

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

Solution 3

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

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

Solution 4

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

Share:
23,906

Related videos on Youtube

shotmeinthehead
Author by

shotmeinthehead

Updated on July 09, 2022

Comments

  • shotmeinthehead
    shotmeinthehead almost 2 years
    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().

    • shotmeinthehead
      shotmeinthehead over 3 years
      PS: I'm also new to stackoverflow. So, please tell me what I did wrong :)
  • shotmeinthehead
    shotmeinthehead over 3 years
    what about how to pass the onClick to another screen?
  • Gabriele Mariotti
    Gabriele Mariotti over 3 years
    @FadelFarinsqi for example just use startActivity(intent) as a normal Activity in Android
  • kc_dev
    kc_dev over 2 years
    Intuition tells me Modifier.background(Color) should work here, but Google had other plans in store...