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)
)
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 would like to use the TabRow, but when I click the background has a ripple effect that I do not want. Is there a way to change this? I have the Tab's selectedContectColor equal to the same background color of the page, but I still see a white ripple effect.
TabRow(
modifier = Modifier.height(20.dp),
selectedTabIndex = selectedIndex,
indicator = { tabPositions ->
TabRowDefaults.Indicator(
modifier = Modifier.customTabIndicatorOffset(
currentTabPosition = tabPositions[lazyListState.firstVisibleItemIndex]
tabWidths[lazyListState.firstVisibleItemIndex]
),
color = RED
)
},
backgroundColor = BLACK
) {
tabList.forEachIndexed{ index, tab ->
val selected = (selectedIndex == index)
Tab(
modifier = Modifier
// Have tried different solutions here where there is a .clickable
// and the indication = null, and set interactionSource = remember{
//MutableInteractionSource()}
selected = selected,
selectedContentColor = BLACK,
onClick = {
animateScrollToItem(selectedIndex)
},
text = {
Text("Text Code")
}
)
}
}
You can see in these docs that the selectedContentColor affects the ripple
The ripple is implemented in a selectable modifier defined inside the Tab.
You can't disable it but you can change the appearance of the ripple that is based on a RippleTheme. You can define a custom RippleTheme and apply to your composable with the LocalRippleTheme.
CompositionLocalProvider(LocalRippleTheme provides NoRippleTheme) {
//..TabRow()
}
private object NoRippleTheme : RippleTheme {
#Composable
override fun defaultColor() = Color.Unspecified
#Composable
override fun rippleAlpha(): RippleAlpha = RippleAlpha(0.0f,0.0f,0.0f,0.0f)
}
The shimmer effect is handled by the indication property.
Put it inside the clickable section.
You can create an extension function
inline fun Modifier.noRippleClickable(crossinline onClick: ()->Unit): Modifier = composed {
clickable(indication = null,
interactionSource = remember { MutableInteractionSource() }) {
onClick()
}
}
then simply replace Modifier.clickable {} with Modifier.noRippleClickable {}
This is not a duplicate of Modify ripple color of IconButton in Jetpack Compose This question was diverted in a different way...
I am trying to change the IconButton ripple colour as follows
IconButton(
modifier = modifier
.indication(
interactionSource = remember { MutableInteractionSource() },
indication = rememberRipple(
bounded = false,
radius = 30.dp,
color = colorResource(id = R.color.error_red)
)
)
.background(colorResource(id = R.color.white), shape = CircleShape),
onClick = { onButtonPressed() }
) {
Icon(
painter = painterResource(R.drawable.ic_busy_small_busy),
contentDescription = buttonContentDesc,
)
}
The ripple colour does not change. It stays dark Grey.
Also, Is there a way to change the alpha of the ripple??
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.
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