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().
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)
Update version 1.3.0 :
colors = ButtonDefaults.buttonColors(containerColor = 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")
}
}
Custom Colors
To create a custom color you need the RGB value of that color.
Button(
onClick = { },
colors = ButtonDefaults.buttonColors(
backgroundColor = Color(red = 255, green = 169, blue = 0)
)
) {}
backgroundColor = Color(red = 255, green = 169, blue = 0) is how we change the background color of the button to a custom color
STEP 1: Simple only set bg:
Button(
colors = buttonColors(Color.Red),
onClick = {}
) {
Text(text = "Login")
}
Full set colors:
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = Color.Red,
contentColor = Color.Green,
disabledBackgroundColor = Color.Yellow,
disabledContentColor = Color.Magenta
),
onClick = {}
) {
Text(text = "Login")
}
STEP 2 (optional): but this best practice
Material 2 case:
Color.Red change MaterialTheme.colors.primary
Material 3 case:
Color.Red change MaterialTheme.colorScheme.tertiaryContainer
Related
How to set Background Color for Material3 Card in Android Compose?
Piggy backing fro this question. The answers tells how to set a background color.
When material3 card is pressed, it changes color with a ripple effect.
But how can I change the effect color when it is pressed?
CardDefaults.cardColors(....) doesn't do it
The Card with the onClick variant uses internally an indication = rememberRipple(). This creates and remembers a Ripple using values provided by RippleTheme.
You can provide a custom LocalRippleTheme to override the default behaviour:
CompositionLocalProvider(LocalRippleTheme provides GreenRippleTheme) {
Card(
onClick = { /* Do something */ },
modifier = Modifier.size(width = 180.dp, height = 100.dp)
) {
//Card content
}
}
with:
private object GreenRippleTheme : RippleTheme {
#Composable
override fun defaultColor() = Color.Green
#Composable
override fun rippleAlpha(): RippleAlpha = RippleTheme.defaultRippleAlpha(
Color.Green,
lightTheme = true
)
}
Otherwise you can use the clickable modifier:
val interactionSource = remember { MutableInteractionSource() }
Card(
modifier = Modifier
.size(width = 180.dp, height = 100.dp)
.clickable (
onClick = { /* Do something */ },
interactionSource = interactionSource,
indication = rememberRipple(color = Green )
)
) {
//Card content
}
Finally if you want to modify the background color when the Card is pressed (not the ripple effect) you can use:
val interactionSource = remember { MutableInteractionSource() }
val isPressed by interactionSource.collectIsPressedAsState()
val backgroundColor = if (isPressed) Yellow else MaterialTheme.colorScheme.surfaceVariant
Card(
interactionSource = interactionSource,
onClick = { /* Do something */ },
modifier = Modifier
.size(width = 180.dp, height = 100.dp),
colors = CardDefaults.cardColors(
containerColor = backgroundColor
)
) {
//Card content
}
You can use the "onClick" property of the Card component to change the color when it is pressed. To do this, you can define a state variable to track the current color of the card and toggle it on click. For example:
var cardColor by remember { mutableStateOf(Color.White) }
Card(
color = cardColor,
onClick = {
cardColor = if (cardColor == Color.White) Color.Green else Color.White
}
...
)
Alternatively, you can define the ripple color in the Modifier property of the Card component. For example:
Card(
color = Color.White,
modifier = Modifier.clickable(onClick = {
// logic to change color
}).ripple(color = Color.Green),
...
)
I am working on ripple effect in jetpack compose. I provide my color and after clicking on view it show some time after that different type of color showing like dark grey on press state.
binding.itemComposable.setContent {
Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(12.dp)) {
val options = getOptions()
options.forEachIndexed { _, optionText ->
val interactionSource = remember { MutableInteractionSource() }
val isPressed by interactionSource.collectIsPressedAsState()
val backgroundColor = if (isPressed) DuckEggBlue else OffWhite
val textColor = if (isPressed) TealBlue else Slate
val borderWidth = if (isPressed) 1.dp else 0.dp
val borderColor = if (isPressed) Aqua else OffWhite
Surface(
onClick = {
logE("Item Click")
},
shape = RoundedCornerShape(4.dp),
border = BorderStroke(borderWidth, borderColor),
interactionSource = interactionSource
) {
Text(
modifier = Modifier
.fillMaxWidth()
.background(backgroundColor)
.padding(16.dp),
text = optionText,
style = Typography.h3,
fontWeight = FontWeight.Medium,
color = textColor
)
}
}
}
}
Expected Output
Above image is see clearly ripple effect.
Actual output
I cannot add image, instead i added my youtube video. Please have a look.
val DuckEggBlue = Color(0xFFF0FCFC)
This is my color which I am using.
Can anyone guide me what is wrong here.
UPDATE
I tried from this doc
#Immutable
private object SecondaryRippleTheme : RippleTheme {
#Composable
override fun defaultColor() = RippleTheme.defaultRippleColor(
contentColor = DuckEggBlue,
lightTheme = true
)
#Composable
override fun rippleAlpha() = RippleTheme.defaultRippleAlpha(
contentColor = DuckEggBlue,
lightTheme = true
)
}
In my code
CompositionLocalProvider(LocalRippleTheme provides SecondaryRippleTheme) {
Text(
modifier = Modifier
.fillMaxWidth()
.background(OffWhite)
.padding(16.dp),
text = optionText,
style = Typography.h3,
fontWeight = FontWeight.Medium,
color = textColor
)
}
but nothing works. It still grey ripple effect.
I tried many options Box, Surface, using PointerInput to delay but maybe visually it doesn't work when you change background color with same ripple
Surface(
modifier = Modifier
.indication(
interactionSource = interactionSource,
rememberRipple(
color = Color.Cyan
)
)
,
shape = RoundedCornerShape(4.dp),
contentColor = backgroundColor,
border = BorderStroke(borderWidth, borderColor),
onClick = {},
interactionSource = interactionSource
) {
Text(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
text = optionText,
fontWeight = FontWeight.Medium,
color = textColor
)
}
With Color.Cyan
If you don't change color when pressed ripple is more apparent but your color is too light to apply for ripple as i tested with other colors.
with PointerInput I tried by changing is pressed to Boolean with initial false value
Modifier.pointerInput(Unit) {
detectTapGestures(onPress = { offset ->
isPressed = true
val press = PressInteraction.Press(offset)
// delay(50)
interactionSource.emit(press)
tryAwaitRelease()
interactionSource.emit(PressInteraction.Release(press))
isPressed = false
})
}
I want to set the gradient background of my TopAppBar:
My code:
TopAppBar(
modifier = Modifier.background(
Brush.horizontalGradient(
colors = listOf(
Color.Red,
Color.Green
)
)
),
title = { Text("UI Components") },
backgroundColor = Color.Transparent
)
Result:
I found this post: Jetpack Compose Button with gradient background? for button - so I set backgroundColor transparent and custom background via a modifier. Sadly in my case, there is an additional shadow around text which I don't know how to remove. What should I change or maybe TopAppBar is just not designed to be used using gradient and I should write something completely custom?
This shadow is caused by default elevation. Set it to zero:
TopAppBar(
modifier = Modifier.background(
Brush.horizontalGradient(
colors = listOf(
Color.Red,
Color.Green
)
)
),
title = { Text("UI Components") },
backgroundColor = Color.Transparent,
elevation = 0.dp
)
In TopAppBar from Material3 like this:
TopAppBar(
modifier = Modifier.background(
Brush.horizontalGradient(
colors = listOf(
Color.Red,
Color.Green
)
)
),
title = { Text("UI Components") },
colors = TopAppBarDefaults.smallTopAppBarColors(containerColor = Color.Transparent, navigationIconContentColor = onPrimaryColor, titleContentColor = onPrimaryColor),
)
notice of containerColor = Color.Transparent
BottomNavigationBar() can only take the background and contentColor but there is no option for tint color.
If you want to change the tint color of the image then you can use the colorFilter property of the Image
Image(
painter = painterResource(R.drawable.ic_arrow_details),
contentDescription = "",
colorFilter = ColorFilter.tint(Color.Black)
)
If you want to remove completly the tint color and you would like to use icon's colors then try with: tint = Color.Unspecified
For example:
Icon(
modifier = Modifier.size(34.dp),
imageVector = ImageVector.vectorResource(id = R.drawable.ic_your_icon),
contentDescription = "Some icon",
tint = Color.Unspecified
)
In case you didn't want to change content color, and wanted to have an individual color for specific Icon, there is a tint property. Like:
Icon(
Icons.Filled.PushPin, "",
tint = MaterialTheme.colors.secondary
)
But as others said, you can use unselectedContentColor and selectedContentColor in your NavigationItem.
For BottomNavigation, you need to provide BottomNavigationItem to construct it, while constructing BottomNavigationItem, you can use Icon with tint as resource like below
BottomNavigation() {
BottomNavigationItem(icon = {
Icon(asset = vectorResource(id = R.drawable.homeBottomNav), tint = Color.Blue) //this is tint
}, selected = true, onClick = {})
}
With the 1.0.0 (tested with 1.0.0-beta06) in the BottomNavigationItem you can define the attributes:
selectedContentColor
unselectedContentColor
Something like:
BottomNavigation(backgroundColor = Color.White) {
BottomNavigationItem(
selectedContentColor = Color.Red,
unselectedContentColor = Color.Gray,
icon = {
Icon(Icons.Filled.Add, "contentDescription")
},
selected = true,
onClick = {})
BottomNavigationItem(
selectedContentColor = Color.Red,
unselectedContentColor = Color.Gray,
icon = {
Icon(Icons.Filled.Search, "contentDescription")
},
selected = false,
onClick = {})
}
Also since the default selectedContentColor is based on the LocalContentColor.current you can also use something like:
BottomNavigation(backgroundColor = Color.White) {
CompositionLocalProvider(LocalContentColor provides Color.Red) {
BottomNavigationItem(
icon = {
Icon(Icons.Filled.Add, "contentDescription")
},
selected = true,
onClick = {})
BottomNavigationItem(
icon = {
Icon(Icons.Filled.Search, "contentDescription")
},
selected = false,
onClick = {})
}
}
You can use unselectedContentColor and selectedContentColor.
BottomNavigationItem(
unselectedContentColor = Color.Black,
selectedContentColor = Color.Red,
icon = {
Icon(
painter = painterResource(id = screen.iconResourceId),
contentDescription = null)
},
selected = currentRoute == screen.route,
onClick = { }
)
selectedContentColor changes color of Text and Icon, but not Image() Composable. So if you want to keep color of multicolor-icon when selected, one should use Image(). Also you want to make it grayscale when unselected, you can use colorFilter.
Plus if you don't want to change color of Text/Icon, then you can use Color.Unspecified.
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?
Try this:
Button(
onClick = {},
colors = ButtonConstants.defaultButtonColors(backgroundColor = Color.Yellow)
) {
/**/
}
You can use the ButtonDefaults.buttonColors using the backgroundColor property:
Something like:
Button(
onClick = { },
colors = ButtonDefaults.buttonColors(
backgroundColor = Color.Red)
)