Select and deselect switches

I am developing a quiz application that consists of a question and three parameters, and I use radio buttons for these parameters. My query: I click on one of the parameters and whenever I want to click on another option, the previous option remains in the checked state, and it does the same when I click on the third option. I need a solution in which it behaves like a natural switch, only one parameter is checked at any given time.

+4
source share
3 answers

You can group RadioButtons with RadioGroup. Here is an example of how you can group RadioButtons using RadioGroup

<RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:text="RadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radio0" android:checked="true"></RadioButton> <RadioButton android:text="RadioButton" android:layout_width="wrap_content" android:id="@+id/radioButton1" android:layout_height="wrap_content"></RadioButton> </RadioGroup> 

For more information, you can refer to this document.

+4
source

You need to place the radio buttons in a group of radio stations, which then provides you with the "only one selected at any time" functionality. See here for more information, for example:

The important thing is that RadioButtons are grouped together with the RadioGroup element, so that no more than one can be selected at a time. This logic is automatically processed by the Android system. When one RadioButton inside a group is selected, all others are automatically canceled.

+1
source

set the radio buttons to onclicklistener and manually change the state of each switch. For example, if you clicked the first radio button, first set it to the selected and the rest so that it is not selected.

-1
source

All Articles