Use radio groups when exactly one option should be selected from a set.
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.unit.dp
import com.composables.ui.components.Radio
import com.composables.ui.components.RadioGroup
import com.composables.ui.components.Text
@Composable
fun RadioGroupExample() {
var selected by remember { mutableStateOf("Weekly") }
RadioGroup(value = selected, onValueChange = { selected = it }) {
Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
Radio("Daily") { Text("Daily") }
Radio("Weekly") { Text("Weekly") }
Radio("Monthly") { Text("Monthly") }
}
}
}Installation
implementation("com.composables:ui:0.1.0")Add the required dependencies
implementation("com.composables:composeunstyled:2.7.0")
Copy and paste the following sources into your project
components/RadioGroup.kt
package com.composables.ui.components
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import com.composables.ui.theme.alphas
import com.composables.ui.theme.borderColor
import com.composables.ui.theme.colors
import com.composables.ui.theme.controlColor
import com.composables.ui.theme.defaultIndication
import com.composables.ui.theme.disabledAlpha
import com.composables.ui.theme.ringColor
import com.composables.ui.theme.indications
import com.composables.ui.theme.onPanelColor
import com.composables.ui.theme.primaryColor
import com.composeunstyled.ProvideContentColor
import com.composeunstyled.SelectedIndicator
import com.composeunstyled.UnstyledRadioButton
import com.composeunstyled.UnstyledRadioGroup
import com.composeunstyled.buildModifier
import com.composeunstyled.theme.Theme
/**
* A single-selection group that coordinates radio options.
* @param value Currently selected value for the radio group or the value represented by a radio.
* @param onValueChange Called when the selected value changes.
* @param modifier Modifier applied to the component.
* @param accessibilityLabel Accessible label announced for the radio group.
* @param content Composable content displayed by the component.
*/
@Composable
fun <T> RadioGroup(
value: T?,
onValueChange: (T) -> Unit,
modifier: Modifier = Modifier,
accessibilityLabel: String? = null,
content: @Composable () -> Unit,
) {
UnstyledRadioGroup(value, onValueChange, modifier, accessibilityLabel) {
content()
}
}
/**
* A selectable option inside a RadioGroup.
* @param value Currently selected value for the radio group or the value represented by a radio.
* @param modifier Modifier applied to the component.
* @param enabled Whether the radio option can be interacted with.
* @param interactionSource Interaction source used for focus and press state.
* @param content Composable content displayed by the component.
*/
@Composable
fun <T> Radio(
value: T,
modifier: Modifier = Modifier,
enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: (@Composable RowScope.() -> Unit)? = null,
) {
val rowShape = RoundedCornerShape(8.dp)
UnstyledRadioButton(
value = value,
enabled = enabled,
interactionSource = interactionSource,
modifier = modifier
.clip(rowShape)
.padding(2.dp)
.then(buildModifier { if (!enabled) add(Modifier.alpha(Theme[alphas][disabledAlpha])) }),
) {
Row(
horizontalArrangement = Arrangement.spacedBy(10.dp),
verticalAlignment = Alignment.CenterVertically,
) {
SelectedIndicator(
enter = fadeIn(),
exit = fadeOut(),
indication = Theme[indications][defaultIndication],
modifier = Modifier
.focusRing(
interactionSource = interactionSource,
color = Theme[colors][ringColor],
shape = CircleShape,
)
.clip(CircleShape)
.background(Theme[colors][controlColor], CircleShape)
.border(1.dp, Theme[colors][borderColor], CircleShape)
.size(20.dp)
) {
Box(
modifier = Modifier.size(20.dp),
contentAlignment = Alignment.Center
) {
Box(Modifier.size(10.dp).background(Theme[colors][primaryColor], CircleShape))
}
}
if (content != null) {
ProvideContentColor(Theme[colors][onPanelColor]) {
content()
}
}
}
}
}components/Utils.kt
package com.composables.ui.components
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.foundation.interaction.InteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.composables.ui.theme.colors
import com.composables.ui.theme.ringColor
import com.composeunstyled.FocusRingVisibility
import com.composeunstyled.collectIsFocusVisibleAsState
import com.composeunstyled.outline
import com.composeunstyled.theme.Theme
@Composable
fun Modifier.focusRing(
interactionSource: InteractionSource,
width: Dp = 2.dp,
color: Color = Theme[colors][ringColor],
shape: Shape = RectangleShape,
offset: Dp = 0.dp,
visibility: FocusRingVisibility = FocusRingVisibility.FocusVisible,
): Modifier {
val showFocusRing by if (visibility == FocusRingVisibility.FocusVisible) {
interactionSource.collectIsFocusVisibleAsState()
} else {
interactionSource.collectIsFocusedAsState()
}
val animatedWidth by animateDpAsState(
targetValue = if (showFocusRing) width else 0.dp,
animationSpec = tween(durationMillis = 120),
label = "FocusRingWidth",
)
return this then Modifier.outline(
width = animatedWidth,
color = color,
shape = shape,
offset = offset,
)
}
@Composable
fun Modifier.bouncyPress(
interactionSource: InteractionSource,
enabled: Boolean = true,
pressedScale: Float = 0.98f,
): Modifier {
val pressed by interactionSource.collectIsPressedAsState()
val scale by animateFloatAsState(
targetValue = if (enabled && pressed) pressedScale else 1f,
animationSpec = spring(
dampingRatio = Spring.DampingRatioMediumBouncy,
stiffness = Spring.StiffnessMediumLow,
),
label = "BouncyPressScale",
)
return this then Modifier.graphicsLayer {
scaleX = scale
scaleY = scale
}
}Examples
Disabled
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.dp
import com.composables.ui.components.Radio
import com.composables.ui.components.RadioGroup
import com.composables.ui.components.Text
@Composable
fun DisabledRadioGroupExample() {
val selected = "Daily"
RadioGroup(value = selected, onValueChange = {}) {
Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
Radio("Daily") { Text("Daily") }
Radio("Weekly", enabled = false) { Text("Weekly") }
Radio("Monthly", enabled = false) { Text("Monthly") }
}
}
}API Reference
RadioGroup
A single-selection group that coordinates radio options.
@Composable
fun <T> RadioGroup(
value: T?,
onValueChange: (T) -> Unit,
modifier: Modifier = Modifier,
accessibilityLabel: String? = null,
content: @Composable () -> Unit,
)
| Parameter | Type | Description |
|---|---|---|
value |
T? |
Currently selected value for the radio group or the value represented by a radio. |
onValueChange |
(T) -> Unit |
Called when the selected value changes. |
modifier |
Modifier |
Modifier applied to the component. |
accessibilityLabel |
String? |
Accessible label announced for the radio group. |
content |
@Composable () -> Unit |
Composable content displayed by the component. |
Radio
A selectable option inside a RadioGroup.
@Composable
fun <T> Radio(
value: T,
modifier: Modifier = Modifier,
enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: (@Composable RowScope.() -> Unit)? = null,
)
| Parameter | Type | Description |
|---|---|---|
value |
T |
Currently selected value for the radio group or the value represented by a radio. |
modifier |
Modifier |
Modifier applied to the component. |
enabled |
Boolean |
Whether the radio option can be interacted with. |
interactionSource |
MutableInteractionSource |
Interaction source used for focus and press state. |
content |
(@Composable RowScope.() -> Unit)? |
Composable content displayed by the component. |