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")
}
}
Related videos on Youtube
Author by
shotmeinthehead
Updated on July 09, 2022Comments
-
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 inbackgroundColor
and asset in Image().-
shotmeinthehead over 3 yearsPS: I'm also new to stackoverflow. So, please tell me what I did wrong :)
-
-
shotmeinthehead over 3 yearswhat about how to pass the onClick to another screen?
-
Gabriele Mariotti over 3 years@FadelFarinsqi for example just use
startActivity(intent)
as a normalActivity
in Android -
kc_dev over 2 yearsIntuition tells me
Modifier.background(Color)
should work here, but Google had other plans in store...