Skip to content
Button·Free

Outlined button (Compose)

Material 3 outlined button for secondary actions such as Cancel. You can change the border and text colours, and add an optional leading icon.

Free to install.

If PocketCode is installed on your Android device, this opens the page in the app.

Preview

OutlinedActionButton.kt · 52 lines
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

/**
 * Material 3 outlined button for secondary actions (Cancel, Back, Learn more).
 *
 * [borderColor] and [contentColor] default to the theme; pass your own to match a brand colour.
 * When disabled, the border fades to 38 % like the rest of Material 3. Only needs
 * androidx.compose.material3.
 *
 * OutlinedActionButton(text = "Cancel", onClick = onDismiss)
 */
@Composable
fun OutlinedActionButton(
    text: String,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    borderColor: Color = MaterialTheme.colorScheme.outline,
    contentColor: Color = MaterialTheme.colorScheme.primary,
    borderWidth: Dp = 1.dp,
    leadingIcon: ImageVector? = null,
) {
    OutlinedButton(
        onClick = onClick,
        modifier = modifier.heightIn(min = 48.dp),
        enabled = enabled,
        border = BorderStroke(borderWidth, if (enabled) borderColor else borderColor.copy(alpha = 0.38f)),
        colors = ButtonDefaults.outlinedButtonColors(contentColor = contentColor),
    ) {
        if (leadingIcon != null) {
            // Decorative: the text already says what the button does.
            Icon(imageVector = leadingIcon, contentDescription = null, modifier = Modifier.size(ButtonDefaults.IconSize))
            Spacer(Modifier.width(ButtonDefaults.IconSpacing))
        }
        Text(text)
    }
}

Changes in 1.0.0

First version.

Related