Details: I have 2 radio groups with some radio buttons.
What i want: User only select one radio button from 2 groups.
What i have done:
rgOne.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
try {
val checkedRadioButtonId: Int = rgOne.checkedRadioButtonId
if (checkedRadioButtonId != null) {
oneItem = oneList[checkedRadioButtonId]
healthy = oneItem.code!!
AppGlobal.displayShortToast(
this, "You selected: " + oneItem.title
)
if (rgTwo.checkedRadioButtonId != -1) {
rgTwo.clearCheck()
}
}
} catch (e: Exception) {
e.printStackTrace()
}
})
rgTwo.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
try {
val checkedRadioButtonId: Int = rgTwo.checkedRadioButtonId
if (checkedRadioButtonId != null) {
twoItem = twoList[checkedRadioButtonId]
unhealthy = twoItem.code!!
AppGlobal.displayShortToast(
this, "You selected: " + twoItem.title
)
if (rgOne.checkedRadioButtonId != -1) {
rgOne.clearCheck()
}
}
} catch (e: Exception) {
e.printStackTrace()
}
})
Facing the issue: When i call this rgOne.clearCheck() OR rgTwo.clearCheck() ,call whole radio group clicked listener agin and uncheck what i checked. working perfect for first time like when nothig is selected.
What is wrong with this code and what i need to change.