I have two horizontal radiogroups with radiobuttons and I want to check only one of the radiobuttons but since they are in different radiogroups it allows the radiobutton from the other group to be checked too!Is there any way that when I check only one radiobutton from both groups?
Here is the code below:
<RadioGroup
android:id="@ id/rgSatisfied"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/linearLayout">
<RadioGroup
android:id="@ id/rg1"
android:layout_width="383dp"
android:layout_height="47dp"
android:layout_marginLeft="5dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/linearLayout">
<RadioButton
android:id="@ id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@ id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="RadioButton" />
<RadioButton
android:id="@ id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="RadioButton" />
</RadioGroup>
<RadioGroup
android:id="@ id/rg2"
android:layout_width="378dp"
android:layout_height="49dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="4dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/rg1">
<RadioButton
android:id="@ id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@ id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="RadioButton" />
<RadioButton
android:id="@ id/other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Other" />
</RadioGroup>
</RadioGroup>
<RadioGroup
android:id="@ id/rgSomewhat"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/linearLayout">
<RadioGroup
android:id="@ id/rg3"
android:layout_width="383dp"
android:layout_height="47dp"
android:layout_marginLeft="5dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/linearLayout">
<RadioButton
android:id="@ id/radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample1" />
<RadioButton
android:id="@ id/radioButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Sample2" />
<RadioButton
android:id="@ id/radioButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Sample3" />
</RadioGroup>
<RadioGroup
android:id="@ id/rg4"
android:layout_width="378dp"
android:layout_height="49dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="4dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/rg1">
<RadioButton
android:id="@ id/radioButton10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample4" />
<RadioButton
android:id="@ id/radioButton11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Sample5" />
<RadioButton
android:id="@ id/other2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Other" />
</RadioGroup>
</RadioGroup>
<RadioGroup
android:id="@ id/rgDissatisfied"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/linearLayout">
<RadioGroup
android:id="@ id/rg5"
android:layout_width="383dp"
android:layout_height="47dp"
android:layout_marginLeft="5dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/linearLayout">
<RadioButton
android:id="@ id/radioButton13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@ id/radioButton14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="RadioButton" />
<RadioButton
android:id="@ id/radioButton15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="RadioButton" />
</RadioGroup>
<RadioGroup
android:id="@ id/rg6"
android:layout_width="378dp"
android:layout_height="49dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="4dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/rg1">
<RadioButton
android:id="@ id/radioButton16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@ id/radioButton17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="RadioButton" />
<RadioButton
android:id="@ id/other3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Other" />
</RadioGroup>
</RadioGroup>
CodePudding user response:
You can solve your problem by listening to changes to both RadioGroups
.
When a RadioButton
in one RadioGroup
is checked, just un-check all RadioButtons
in the second RadioGroup
. This is tricky, as the un-checking triggers the listener again, crashing your app. In order to prevent that, you have to add an additional condition: That the RadioButton
that triggered the event got checked, not un-checked.
Here's the code:
val groupOne = findViewById<RadioGroup>(R.id.radio_group_one)
val groupTwo = findViewById<RadioGroup>(R.id.radio_group_two)
val checkedChangeListener =
RadioGroup.OnCheckedChangeListener { group, checkedId ->
val checkedButton = group.findViewById<RadioButton>(checkedId)
if (group == groupOne && checkedButton?.isChecked == true) {
groupTwo.clearCheck()
}
if (group == groupTwo && checkedButton?.isChecked == true) {
groupOne.clearCheck()
}
}
groupOne.setOnCheckedChangeListener(checkedChangeListener)
groupTwo.setOnCheckedChangeListener(checkedChangeListener)