How would I create a set of three linked buttons that only allow one button to be selected and highlighted at a time? When button two is selected button one would become unhighlighted and button two would become highlighted
Asked
Active
Viewed 53 times
-2
-
Does this answer your question? [Radio Button and Radio Group in Android](https://stackoverflow.com/questions/65987623/radio-button-and-radio-group-in-android) – Haider Saleem Aug 23 '22 at 17:00
2 Answers
2
You can use the MaterialButtonToggleGroup
provided by the material components library.
Use the android:orientation="vertical"
to have a vertical orientation and the singleSelection="true"
attribute to have a single selection as described in this question.
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleButton"
android:orientation="vertical"
app:singleSelection="true"
app:selectionRequired="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
/>
<Button
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
/>
<Button
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>

Gabriele Mariotti
- 320,139
- 94
- 887
- 841