Skip to content

Commit 0d07b08

Browse files
authored
Add single option radio button example (#401)
* Add single option radio button example * Apply Spotless * Apply Spotless --------- Co-authored-by: jakeroseman <[email protected]>
1 parent 6f61cb5 commit 0d07b08

File tree

1 file changed

+77
-0
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/components

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compose.snippets.components
18+
19+
import androidx.compose.foundation.layout.Column
20+
import androidx.compose.foundation.layout.Row
21+
import androidx.compose.foundation.layout.fillMaxWidth
22+
import androidx.compose.foundation.layout.height
23+
import androidx.compose.foundation.layout.padding
24+
import androidx.compose.foundation.selection.selectable
25+
import androidx.compose.foundation.selection.selectableGroup
26+
import androidx.compose.material3.MaterialTheme
27+
import androidx.compose.material3.RadioButton
28+
import androidx.compose.material3.Text
29+
import androidx.compose.runtime.Composable
30+
import androidx.compose.runtime.mutableStateOf
31+
import androidx.compose.runtime.remember
32+
import androidx.compose.ui.Alignment
33+
import androidx.compose.ui.Modifier
34+
import androidx.compose.ui.semantics.Role
35+
import androidx.compose.ui.tooling.preview.Preview
36+
import androidx.compose.ui.unit.dp
37+
38+
// [START android_compose_components_radiobuttonsingleselection]
39+
@Composable
40+
fun RadioButtonSingleSelection(modifier: Modifier = Modifier) {
41+
val radioOptions = listOf("Calls", "Missed", "Friends")
42+
val (selectedOption, onOptionSelected) = remember { mutableStateOf(radioOptions[0]) }
43+
// Note that Modifier.selectableGroup() is essential to ensure correct accessibility behavior
44+
Column(modifier.selectableGroup()) {
45+
radioOptions.forEach { text ->
46+
Row(
47+
Modifier
48+
.fillMaxWidth()
49+
.height(56.dp)
50+
.selectable(
51+
selected = (text == selectedOption),
52+
onClick = { onOptionSelected(text) },
53+
role = Role.RadioButton
54+
)
55+
.padding(horizontal = 16.dp),
56+
verticalAlignment = Alignment.CenterVertically
57+
) {
58+
RadioButton(
59+
selected = (text == selectedOption),
60+
onClick = null // null recommended for accessibility with screen readers
61+
)
62+
Text(
63+
text = text,
64+
style = MaterialTheme.typography.bodyLarge,
65+
modifier = Modifier.padding(start = 16.dp)
66+
)
67+
}
68+
}
69+
}
70+
}
71+
// [END android_compose_components_radiobuttonsingleselection]
72+
73+
@Preview
74+
@Composable
75+
private fun RadioButtonSingleSelectionPreview() {
76+
RadioButtonSingleSelection()
77+
}

0 commit comments

Comments
 (0)