I have a custom view for radio buttons in the XML layout file with ConstraintLayout. I need to group those radio buttons. But when I try to envelop that ConstraintLayout with the Radio group, it does not group the buttons and all options are getting selected.

- 7,947
- 5
- 29
- 51

- 3,248
- 6
- 30
- 50
-
Why not make a custom view? Your layout is way beautiful to use the plain old RadioGroup logic and there are lot of articles to give you that allow "one of many" selection feature you get in a radio group – gtxtreme Sep 07 '21 at 09:12
-
@gtxtreme Do you mean that using a radio group for this custom view is not possible using old RadioGroup logic? – Sweta Jain Sep 07 '21 at 09:17
-
I have limited experience with RadioGroup as such but I know we use it for that "just one of many" selection feature and the customisation in RadioGroup is limited as far as the look and feel is concerned – gtxtreme Sep 07 '21 at 09:21
-
https://bitbucket.org/ManuelMato/customradiobutton/src/develop/app/src/ Did you try looking at this? Seems like something similar to your use case – gtxtreme Sep 07 '21 at 09:23
3 Answers
RadioButtons need to be the direct children of a RadioGroup in order for the grouping functionality to work. A RadioGroup is just a special LinearLayout that implements the grouping functionality for all the children added that are RadioButtons. You can simply implement your own grouping functionality by adding the RadioButtons to a List and then implementing OnCheckedChangeListener and setting it on each of the RadioButtons as follows:
private final List<RadioButton> radioButtons = new ArrayList<>();
private int checkedId;
private void setRadioButton(RadioButton radioButton) {
radioButtons.add(radioButton);
radioButton.setOnCheckedChangeListener(mOnCheckedChangeListener);
}
private final CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener =
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checkedId = buttonView.getId();
}
for (RadioButton radioButton : radioButtons) {
if (radioButton.getId() != checkedId) {
radioButton.setChecked(false);
}
}
}
};

- 144
- 7
you can set false the checked of icon in the defult running like this code in layout xml
android:checked="false"
and in coding you can call binding.rdiobutton.setonclicklistenert{ binding.btnclick.setonclicklistener)

- 1
- 1
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 02 '22 at 19:13
Use the following code in kotlin
radioGroup.check(id)
First, you write the name of the radio group, then instead of the ID, put the ID of the radio button,
For example, it is like this in my project
rg_licenseType.check(R.id.rb_three)

- 2,414
- 4
- 32
- 72

- 11
- 2