Composables
Docs Login
Pairel

Radio Group

Jetpack Compose Component

Download
package com.composables.pairel.components

import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
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.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import com.composables.compose.ripple.rememberRippleIndication
import com.composables.pairel.theme.Space
import com.composables.pairel.theme.Theme
import com.composeunstyled.SelectedIndicator
import com.composeunstyled.UnstyledRadioButton
import com.composeunstyled.UnstyledRadioGroup

@Composable
fun RadioGroup(
    label: String,
    options: List<String>,
    initialValue: String,
    onValueChange: (String) -> Unit,
    modifier: Modifier = Modifier,
) {
    var value by remember(initialValue) { mutableStateOf(initialValue) }
    UnstyledRadioGroup(
        value = value,
        onValueChange = {
            value = it
            onValueChange(it)
        },
        modifier = modifier,
        accessibilityLabel = label,
    ) {
        Column(verticalArrangement = Arrangement.spacedBy(Space)) {
            options.forEach { option ->
                val selected = option == value
                val interactionSource = remember { MutableInteractionSource() }
                UnstyledRadioButton(
                    value = option,
                    modifier = Modifier.fillMaxWidth().clip(Theme.shapes.small),
                    interactionSource = interactionSource,
                    indication = rememberRippleIndication(color = Theme.colors.accent),
                ) {
                    Row(
                        modifier = Modifier.fillMaxWidth().padding(Space * 3),
                        verticalAlignment = Alignment.CenterVertically,
                        horizontalArrangement = Arrangement.spacedBy(Space * 3),
                    ) {
                        Box(
                            Modifier.size(22.dp)
                                .clip(CircleShape)
                                .background(
                                    if (selected) Theme.colors.accent
                                    else Theme.colors.surfaceVariant
                                ),
                            contentAlignment = Alignment.Center,
                        ) {
                            SelectedIndicator(
                                enter = fadeIn() + scaleIn(),
                                exit = fadeOut() + scaleOut(),
                            ) {
                                Box(
                                    Modifier.size(8.dp)
                                        .clip(CircleShape)
                                        .background(Theme.colors.onAccent)
                                )
                            }
                        }
                        Text(option)
                    }
                }
            }
        }
    }
}

More components from Pairel

by Alex Styl

Search screens and components

Screen flows

Screens

UI Elements

One Android lesson, every Wednesday

Building Android apps?

What Alex learned building Android apps for 14 years, so you don't have to. Reads in under a minute.

Join 3,500+ Android developers. Unsubscribe any time.

You're subscribed

The next lesson lands in on Wednesday.