Calling this line once, inside onCreate() of Activity or onCreateView() of Fragment:

   rgOne.setOnCheckedChangeListener(onCheckedChangeListener)
   rgTwo.setOnCheckedChangeListener(onCheckedChangeListener)

Declare this variable:

private val onCheckedChangeListener =  
    RadioGroup.OnCheckedChangeListener { group, checkedId ->
        if (group!!.id == R.id.rgOne) {
            try {
                AppLog.e("Call healthy")
                val checkedRadioButtonId: Int = rgOne.checkedRadioButtonId
                if (checkedRadioButtonId != null) {
              
                    if (rgTwo.checkedRadioButtonId != -1) {
                        resetRadioGroup(rgTwo)
                        
                    }


                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

        } else {
            try {
                AppLog.e("Call Unhealthy")
                val checkedRadioButtonId: Int = rgTwo.checkedRadioButtonId
                if (checkedRadioButtonId != null) {
                   
                    if (rgOne.checkedRadioButtonId != -1) {
                        resetRadioGroup(rgOne)
                        
                    }


                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }

    }

Function for reset radiogroup:

fun resetRadioGroup(rgType: RadioGroup) {
    rgType.setOnCheckedChangeListener(null)
    rgType.clearCheck()
    rgType.setOnCheckedChangeListener(onCheckedChangeListener)
}

Reference link

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top