Why all RadioButtons added programmatically inside RadioGroup can be selected

Viewed 17

I added my RadioButtons programmatically inside RadioGroup as below:

My main layout

<RadioGroup
        android:id="@+id/rgAddresses"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checkedButton="@+id/first"
        android:orientation="vertical"
        android:layout_marginTop="10dp"
        android:padding="10dp">
</RadioGroup>

And below is the code I used to add radio buttons inside the RadioGroup dynamically:

addressesSelectVM.getAddresses(userPK)?.observe(viewLifecycleOwner){  addresses ->
    for (getAddress in addresses){    
        val addressLayout: View = layoutInflater.inflate(R.layout.user_address_select,binding.rgAddresses,false)
        val name = getAddress.fullName;
        val address = getAddress.address
        val rbName: RadioButton = addressLayout.findViewById(R.id.rbName)
        rbName.text = URLDecoder.decode(name,"utf-8")

        val tvAddress: TextView = addressLayout.findViewById(R.id.tvAddress)
        tvAddress.text = URLDecoder.decode(address,"utf-8")
        binding.rgAddresses.addView(addressLayout)
    }
}

I expected to be able to choose/select only one radio button, but it turned out I can select all the radio buttons inside the RadioGroup rgAddresses. How do I make it so that only one radio button can be selected inside the RadioGroup? Thanks!

0 Answers
Related