Interactive button components for user actions, including filled buttons, outlined buttons, and icon buttons. All button components support full customization of colors, shapes, and interaction states while maintaining Material3 design principles.
import androidx.compose.material3.*
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.IconButton
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.ButtonDefaultsStandard filled Material3 button with elevated appearance and prominent visual weight.
/**
* Material3 filled button component with container background and elevated appearance
* @param onClick Callback invoked when the button is clicked
* @param modifier Modifier to be applied to the button
* @param enabled Whether the button is enabled and can be clicked
* @param shape The shape of the button container
* @param colors Color scheme for the button in different states
* @param elevation Elevation configuration for the button shadow
* @param border Optional border stroke for the button
* @param contentPadding Padding around the button content
* @param interactionSource Source for tracking interaction states
* @param content The content to be displayed inside the button
*/
@Composable
fun Button(
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
shape: Shape = ButtonDefaults.shape,
colors: ButtonColors = ButtonDefaults.buttonColors(),
elevation: ButtonElevation? = ButtonDefaults.buttonElevation(),
border: BorderStroke? = null,
contentPadding: PaddingValues = ButtonDefaults.ContentPadding,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: @Composable RowScope.() -> Unit
)Usage Examples:
// Basic button
Button(
onClick = { /* handle click */ }
) {
Text("Click Me")
}
// Button with icon
Button(
onClick = { /* handle click */ }
) {
Icon(Icons.Default.Add, contentDescription = null)
Spacer(Modifier.width(8.dp))
Text("Add Item")
}
// Custom styled button
Button(
onClick = { /* handle click */ },
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.secondary,
contentColor = MaterialTheme.colorScheme.onSecondary
),
shape = RoundedCornerShape(20.dp)
) {
Text("Custom Button")
}
// Disabled button
Button(
onClick = { /* handle click */ },
enabled = false
) {
Text("Disabled")
}Button with outline border and transparent background, providing secondary visual emphasis.
/**
* Material3 outlined button with border stroke and transparent background
* @param onClick Callback invoked when the button is clicked
* @param modifier Modifier to be applied to the button
* @param enabled Whether the button is enabled and can be clicked
* @param shape The shape of the button container
* @param colors Color scheme for the button in different states
* @param elevation Elevation configuration (typically null for outlined buttons)
* @param border Border stroke for the button outline
* @param contentPadding Padding around the button content
* @param interactionSource Source for tracking interaction states
* @param content The content to be displayed inside the button
*/
@Composable
fun OutlinedButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
shape: Shape = ButtonDefaults.outlinedShape,
colors: ButtonColors = ButtonDefaults.outlinedButtonColors(),
elevation: ButtonElevation? = null,
border: BorderStroke? = ButtonDefaults.outlinedButtonBorder,
contentPadding: PaddingValues = ButtonDefaults.ContentPadding,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: @Composable RowScope.() -> Unit
)Usage Examples:
// Basic outlined button
OutlinedButton(
onClick = { /* handle click */ }
) {
Text("Outlined")
}
// Outlined button with custom border
OutlinedButton(
onClick = { /* handle click */ },
border = BorderStroke(2.dp, MaterialTheme.colorScheme.primary)
) {
Text("Custom Border")
}
// Outlined button with icon
OutlinedButton(
onClick = { /* handle click */ }
) {
Icon(Icons.Default.Settings, contentDescription = null)
Spacer(Modifier.width(8.dp))
Text("Settings")
}Clickable icon button without background, used for toolbar actions and icon-based interactions.
/**
* Material3 icon button for displaying clickable icons without background
* @param onClick Callback invoked when the button is clicked
* @param modifier Modifier to be applied to the button
* @param enabled Whether the button is enabled and can be clicked
* @param colors Color scheme for the button icon in different states
* @param interactionSource Source for tracking interaction states
* @param content The icon content to be displayed
*/
@Composable
fun IconButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
colors: IconButtonColors = IconButtonDefaults.iconButtonColors(),
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: @Composable () -> Unit
)Usage Examples:
// Basic icon button
IconButton(
onClick = { /* handle click */ }
) {
Icon(Icons.Default.Favorite, contentDescription = "Add to favorites")
}
// Icon button with custom colors
IconButton(
onClick = { /* handle click */ },
colors = IconButtonDefaults.iconButtonColors(
contentColor = MaterialTheme.colorScheme.primary
)
) {
Icon(Icons.Default.Share, contentDescription = "Share")
}
// Disabled icon button
IconButton(
onClick = { /* handle click */ },
enabled = false
) {
Icon(Icons.Default.Delete, contentDescription = "Delete")
}Prominent circular button for primary actions, typically positioned in the bottom-right corner.
/**
* Material3 floating action button for primary actions
* @param onClick Callback invoked when the FAB is clicked
* @param modifier Modifier to be applied to the FAB
* @param shape The shape of the FAB container
* @param containerColor Background color of the FAB
* @param contentColor Color of the FAB content
* @param elevation Elevation configuration for the FAB shadow
* @param interactionSource Source for tracking interaction states
* @param content The content to be displayed inside the FAB
*/
@Composable
fun FloatingActionButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
shape: Shape = FloatingActionButtonDefaults.shape,
containerColor: Color = FloatingActionButtonDefaults.containerColor,
contentColor: Color = contentColorFor(containerColor),
elevation: FloatingActionButtonElevation = FloatingActionButtonDefaults.elevation(),
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: @Composable () -> Unit,
)Usage Examples:
// Basic FAB
FloatingActionButton(
onClick = { /* handle click */ }
) {
Icon(Icons.Default.Add, contentDescription = "Add")
}
// FAB with custom colors
FloatingActionButton(
onClick = { /* handle click */ },
containerColor = MaterialTheme.colorScheme.secondary,
contentColor = MaterialTheme.colorScheme.onSecondary
) {
Icon(Icons.Default.Edit, contentDescription = "Edit")
}Default configurations and factory methods for button styling.
object ButtonDefaults {
/** Default shape for filled buttons */
val shape: Shape
/** Default shape for outlined buttons */
val outlinedShape: Shape
/** Default content padding for all button types */
val ContentPadding: PaddingValues
/** Default minimum width for buttons */
val MinWidth: Dp
/** Default minimum height for buttons */
val MinHeight: Dp
/**
* Creates default colors for filled buttons
* @param containerColor Background color of the button
* @param contentColor Color of the button content
* @param disabledContainerColor Background color when disabled
* @param disabledContentColor Content color when disabled
*/
fun buttonColors(
containerColor: Color = Color.Unspecified,
contentColor: Color = Color.Unspecified,
disabledContainerColor: Color = Color.Unspecified,
disabledContentColor: Color = Color.Unspecified,
): ButtonColors
/**
* Creates default colors for outlined buttons
* @param contentColor Color of the button content
* @param disabledContentColor Content color when disabled
*/
fun outlinedButtonColors(
contentColor: Color = Color.Unspecified,
disabledContentColor: Color = Color.Unspecified,
): ButtonColors
/**
* Creates default elevation for buttons
* @param defaultElevation Elevation in normal state
* @param pressedElevation Elevation when pressed
* @param focusedElevation Elevation when focused
* @param hoveredElevation Elevation when hovered
* @param disabledElevation Elevation when disabled
*/
fun buttonElevation(
defaultElevation: Dp = 1.dp,
pressedElevation: Dp = 0.dp,
focusedElevation: Dp = 1.dp,
hoveredElevation: Dp = 3.dp,
disabledElevation: Dp = 0.dp,
): ButtonElevation
/** Default border stroke for outlined buttons */
val outlinedButtonBorder: BorderStroke
}Default configurations for icon buttons.
object IconButtonDefaults {
/**
* Creates default colors for icon buttons
* @param contentColor Color of the icon content
* @param disabledContentColor Content color when disabled
*/
fun iconButtonColors(
contentColor: Color = Color.Unspecified,
disabledContentColor: Color = Color.Unspecified,
): IconButtonColors
}Default configurations for floating action buttons.
object FloatingActionButtonDefaults {
/** Default shape for FABs */
val shape: Shape
/** Default container color for FABs */
val containerColor: Color
/**
* Creates default elevation for FABs
* @param defaultElevation Elevation in normal state
* @param pressedElevation Elevation when pressed
* @param focusedElevation Elevation when focused
* @param hoveredElevation Elevation when hovered
*/
fun elevation(
defaultElevation: Dp = 6.dp,
pressedElevation: Dp = 12.dp,
focusedElevation: Dp = 6.dp,
hoveredElevation: Dp = 8.dp,
): FloatingActionButtonElevation
}interface ButtonColors {
/**
* Represents the container color for this button, depending on [enabled].
* @param enabled whether the button is enabled
*/
fun containerColor(enabled: Boolean): Color
/**
* Represents the content color for this button, depending on [enabled].
* @param enabled whether the button is enabled
*/
fun contentColor(enabled: Boolean): Color
}interface IconButtonColors {
/**
* Represents the content color for this icon button, depending on [enabled].
* @param enabled whether the icon button is enabled
*/
fun contentColor(enabled: Boolean): Color
}interface ButtonElevation {
/**
* Represents the elevation used in a button, depending on [enabled] and [interactionSource].
* @param enabled whether the button is enabled
* @param interactionSource the interaction source for tracking button state
*/
@Composable
fun shadowElevation(enabled: Boolean, interactionSource: InteractionSource): State<Dp>
/**
* Represents the tonal elevation used in a button, depending on [enabled] and [interactionSource].
* @param enabled whether the button is enabled
* @param interactionSource the interaction source for tracking button state
*/
@Composable
fun tonalElevation(enabled: Boolean, interactionSource: InteractionSource): State<Dp>
}interface FloatingActionButtonElevation {
/**
* Represents the shadow elevation used in a FAB, depending on [interactionSource].
* @param interactionSource the interaction source for tracking FAB state
*/
@Composable
fun shadowElevation(interactionSource: InteractionSource): State<Dp>
/**
* Represents the tonal elevation used in a FAB, depending on [interactionSource].
* @param interactionSource the interaction source for tracking FAB state
*/
@Composable
fun tonalElevation(interactionSource: InteractionSource): State<Dp>
}data class BorderStroke(
val width: Dp,
val brush: Brush
) {
constructor(width: Dp, color: Color) : this(width, SolidColor(color))
}