Composables
Docs Login
Pairel

Choice Button

Jetpack Compose Component

Download
package com.composables.pairel.components

import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.semantics.selected
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.composables.compose.ripple.rememberRippleIndication
import com.composables.pairel.theme.ProvideContentColor
import com.composables.pairel.theme.ProvideTextStyle
import com.composables.pairel.theme.Theme
import com.composeunstyled.UnstyledButton

@Composable
fun ChoiceButton(
    selected: Boolean,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    content: @Composable () -> Unit,
) {
    val contentColor = if (selected) Theme.colors.onAccent else Theme.colors.onSurface
    UnstyledButton(
        onClick = onClick,
        modifier =
            modifier
                .clip(Theme.shapes.fullyRounded)
                .background(if (selected) Theme.colors.accent else Theme.colors.surfaceVariant)
                .semantics { this.selected = selected },
        contentPadding = PaddingValues(horizontal = 14.dp, vertical = 12.dp),
        indication = rememberRippleIndication(color = contentColor),
        interactionSource = remember { MutableInteractionSource() },
    ) {
        ProvideContentColor(contentColor) {
            ProvideTextStyle(Theme.typography.label.copy(fontSize = 14.sp), content)
        }
    }
}

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.