I have two radio groups (group 1 and group 2) and contain 3 radio buttons respectively. And I use the guideline to separate left and right column with 50% 50%. Since the two radio group is separated, when user click group 1, I would like clear selected in group 2 if any. Any idea to do it, since I am writing by Kotlin, I found some example in stackoverflow ConstraintLayout, RadioGroup and two columns of RadioButton RadioGroup with two columns which have ten RadioButtons Because the example is in JAVA, but I cannot apply in my Kotlin code. I am a newbie in Kotlin and Java, I am using the Android Studio with "Navigation Drawer Activity", and apply the coding in "Fragment.kt" Anyone have idea on it? thanks!
Here is my Kotlin coding in MainActivity.kt, there is ex exception in the val val listener2--> rg1.setOnCheckedChangeListener(listener1) "Type checking has run into a recursive problem...."
package com.example.pain_testing
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.RadioGroup
import android.util.Log
class MainActivity : AppCompatActivity() {
val rg1 = findViewById(R.id.radioGroup1) as RadioGroup
val rg2 = findViewById(R.id.radioGroup2) as RadioGroup
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rg1.clearCheck(); // this is so we can start fresh, with no selection on both RadioGroups
rg2.clearCheck();
rg1.setOnCheckedChangeListener(listener1); //apply onCheckChangeListener
rg2.setOnCheckedChangeListener(listener2);
}
private val listener1 = object : RadioGroup.OnCheckedChangeListener {
override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {
if (checkedId != -1) {
rg2.setOnCheckedChangeListener(null) // remove the listener before clearing so we don't throw that stackoverflow exception(like Vladimir Volodin pointed out)
rg2.clearCheck() // clear the second RadioGroup!
rg2.setOnCheckedChangeListener(listener2) //reset the listener
Log.e("XXX2", "do the work")
}
}
}
private val listener2 = object : RadioGroup.OnCheckedChangeListener {
override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {
if (checkedId != -1) {
rg1.setOnCheckedChangeListener(null)
rg1.clearCheck()
rg1.setOnCheckedChangeListener(listener1)
Log.e("XXX2", "do the work")
}
}
}
}