From a8cbfa0f675abaead8b229d9bd27083835c31f81 Mon Sep 17 00:00:00 2001 From: jakeroseman Date: Wed, 27 Nov 2024 19:47:27 +0000 Subject: [PATCH 1/3] Add single option radio button example --- ...kotlin-compiler-5020797412157966223.salive | 0 .../snippets/components/RadioButton.kt | 61 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 .kotlin/sessions/kotlin-compiler-5020797412157966223.salive create mode 100644 compose/snippets/src/main/java/com/example/compose/snippets/components/RadioButton.kt diff --git a/.kotlin/sessions/kotlin-compiler-5020797412157966223.salive b/.kotlin/sessions/kotlin-compiler-5020797412157966223.salive new file mode 100644 index 000000000..e69de29bb diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/components/RadioButton.kt b/compose/snippets/src/main/java/com/example/compose/snippets/components/RadioButton.kt new file mode 100644 index 000000000..ddea57181 --- /dev/null +++ b/compose/snippets/src/main/java/com/example/compose/snippets/components/RadioButton.kt @@ -0,0 +1,61 @@ +package com.example.compose.snippets.components + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.selection.selectable +import androidx.compose.foundation.selection.selectableGroup +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.RadioButton +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp + +// [START android_compose_components_radiobuttonsingleselection] +@Composable +fun RadioButtonSingleSelection(modifier: Modifier = Modifier) { + val radioOptions = listOf("Calls", "Missed", "Friends") + val (selectedOption, onOptionSelected) = remember { mutableStateOf(radioOptions[0]) } + // Note that Modifier.selectableGroup() is essential to ensure correct accessibility behavior + Column(modifier.selectableGroup()) { + radioOptions.forEach { text -> + Row( + Modifier + .fillMaxWidth() + .height(56.dp) + .selectable( + selected = (text == selectedOption), + onClick = { onOptionSelected(text) }, + role = Role.RadioButton + ) + .padding(horizontal = 16.dp), + verticalAlignment = Alignment.CenterVertically + ) { + RadioButton( + selected = (text == selectedOption), + onClick = null // null recommended for accessibility with screen readers + ) + Text( + text = text, + style = MaterialTheme.typography.bodyLarge, + modifier = Modifier.padding(start = 16.dp) + ) + } + } + } +} +// [END android_compose_components_radiobuttonsingleselection] + +@Preview +@Composable +private fun RadioButtonSingleSelectionPreview() { + RadioButtonSingleSelection() +} From ac9a7a72854eb8a6b8c12c377e7ff49ffa186730 Mon Sep 17 00:00:00 2001 From: jakeroseman Date: Wed, 27 Nov 2024 20:01:58 +0000 Subject: [PATCH 2/3] Apply Spotless --- .../compose/snippets/components/RadioButton.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/components/RadioButton.kt b/compose/snippets/src/main/java/com/example/compose/snippets/components/RadioButton.kt index ddea57181..93961f245 100644 --- a/compose/snippets/src/main/java/com/example/compose/snippets/components/RadioButton.kt +++ b/compose/snippets/src/main/java/com/example/compose/snippets/components/RadioButton.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.compose.snippets.components import androidx.compose.foundation.layout.Column From d8d4a3b1464cc7df880e87b580b04b05e9e13bfb Mon Sep 17 00:00:00 2001 From: jakeroseman Date: Wed, 4 Dec 2024 18:20:13 +0000 Subject: [PATCH 3/3] Apply Spotless --- .kotlin/sessions/kotlin-compiler-5020797412157966223.salive | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .kotlin/sessions/kotlin-compiler-5020797412157966223.salive diff --git a/.kotlin/sessions/kotlin-compiler-5020797412157966223.salive b/.kotlin/sessions/kotlin-compiler-5020797412157966223.salive deleted file mode 100644 index e69de29bb..000000000