2

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")
        }
    }
}

}

Harry
  • 33
  • 5

3 Answers3

1

Just clear inside a change listener:

radioGroup1.setOnCheckedChangeListener { _, _ -> 
    radioGroup2.clearCheck()
}
charles-allen
  • 3,891
  • 2
  • 23
  • 35
  • Hi Ajahn Charles, I try to edit the question, do you have any idea? – Harry Sep 01 '19 at 13:47
  • Yes! Delete all that code (the stuff in onCreate and both listeners). Then type my answer inside onCreate. It really is that simple! When you write "radioGroup1" make sure you tab-complete it (to import Kotlin synthetic; you rarely need findViewById in Kotlin - or you could press `Alt+Enter` to import). – charles-allen Sep 01 '19 at 14:35
  • Still have exceptions package com.example.pain_testing import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { //val rg1 = findViewById(R.id.radioGroup1) //val rg2 = findViewById(R.id.radioGroup2) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) radioGroup1.setOnCheckedChangeListener { radioGroup2.clearCheck() } } } – Harry Sep 02 '19 at 05:58
  • Apologies; I was too lazy to check. You are required to specify the inputs to the lambda (though you can use _ since you ignore them): `radioGroup1.setOnCheckedChangeListener { _, _ -> radioGroup2.clearCheck() }` – charles-allen Sep 02 '19 at 06:21
0

you could do following:

First initialize the two radio groups like shown below:

val rg1 = findViewById(R.id.radioGroup1) as RadioGroup
val rg2 = findViewById(R.id.radioGroup2) as RadioGroup
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); 

Then create an onCheckChangeListener:

private val listener1 = object : OnCheckedChangeListener() {

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")
    }
}
Lukas
  • 812
  • 1
  • 14
  • 43
0
private lateinit var radioGroup1: RadioGroup
private lateinit var radioGroup2: RadioGroup
private var radioGroupBlock: Boolean = false

    ...

    radioGroup1 = view.findViewById(R.id.radioGroup_1)
    radioGroup2 = view.findViewById(R.id.radioGroup_2)

    ...

    radioGroup1.setOnCheckedChangeListener({ group, checkedId ->
        if (radioGroupBlock)
            return@setOnCheckedChangeListener
        val radio: RadioButton = view.findViewById(checkedId)
        toast(" On checked change : ${radio.text}")
        var id: Int = radioGroup2.checkedRadioButtonId
        if (id!=-1){
            radioGroupBlock = true
            radioGroup2.clearCheck()
            radioGroupBlock = false
        }
    })

    radioGroup2.setOnCheckedChangeListener({ group, checkedId ->
        if (radioGroupBlock)
            return@setOnCheckedChangeListener
        val radio: RadioButton = view.findViewById(checkedId)
        toast(" On checked change : ${radio.text}")
        var id: Int = radioGroup1.checkedRadioButtonId
        if (id!=-1){
            radioGroupBlock = true
            radioGroup1.clearCheck()
            radioGroupBlock = false
        }

    })