androidkotlinradio-buttonandroid-radiobuttonandroid-radiogroup

Whne i call this radiogroupId.clearCheck() this method recall radiogroup click listener


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.


Solution

  • 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