This is inside a custom view class that extends RelativeLayout not inside an Activity or Fragment.
My RadioGroup
exists in my xml
and I'm trying to dynamically add RadioButtons
to it. The only time I've seen it where the mutual exclusivity does not work, is if you do not add an id but I am here. What happens is when I tap a radio button more than 1 button is selected at a time. What am I missing here?
for(String buttonName : mButtonNames){
RadioButton radioButton = new RadioButton(getContext());
radioButton.setId(ViewCompat.generateViewId() );
radioButton.setText(buttonName);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.weight = 1;
radioButton.setLayoutParams(layoutParams);
radioButton.setBackground(mRadioBckgrdDrawable);
radioButton.setButtonDrawable(null);
radioButton.setGravity(Gravity.CENTER);
radioButton.setTextSize(mTextSize);
radioButton.setTextColor(mTextColor);
mRadioGroup.addView(radioButton);
}
Background drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected-->
<item android:drawable="@drawable/two_stage_toggle_button_on"
android:state_checked="true" />
<!-- When not selected-->
<item android:drawable="@drawable/two_stage_toggle_button_off"
android:state_checked="false"/>
</selector>