0

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>
Mike6679
  • 5,547
  • 19
  • 63
  • 108
  • I think you need to use RadioGroup's [addView(View, int, ViewGroup.LayoutParams)](https://developer.android.com/reference/android/widget/RadioGroup.html#addView(android.view.View,%20int,%20android.view.ViewGroup.LayoutParams)). – TheWanderer Oct 03 '18 at 12:34
  • @TheWanderer : Nope tried that, same issue. – Mike6679 Oct 03 '18 at 12:40
  • I think this might be a bug. What's your target API? – TheWanderer Oct 03 '18 at 12:48
  • My Target version is 27 – Mike6679 Oct 03 '18 at 12:54
  • I also tried adding my RadioGroup dynamically, also same issue. Yeah it does seem like a bug – Mike6679 Oct 03 '18 at 13:00
  • I think I've narrowed the issue --- when I do NOT set the background dynamically then the mutually exclusive selection works. I'll include my background drawable above – Mike6679 Oct 03 '18 at 14:22
  • 1
    Try making an XML layout file that only contains a RadioButton. Set the attributes you want in XML then inflate it and assign an ID before adding it to your RadioGroup. – TheWanderer Oct 03 '18 at 14:28
  • When I add from an inflated view the first radio button takes up the entire width of the screen – Mike6679 Oct 03 '18 at 15:59
  • add the weight attribute. It won't suggest it for autocomplete because it's the root element, but it should still work. – TheWanderer Oct 03 '18 at 16:23
  • Ah I got it ( making an XML layout file that only contains a RadioButton) you HAVE to to set the layout parameters dynamically including weight , after inflating the radio button --- yeah its a work around for now, thx @TheWanderer – Mike6679 Oct 03 '18 at 16:24
  • you shouldn't need to. You can still define the weight attribute in XML even the editor doesn't suggest it. – TheWanderer Oct 03 '18 at 16:26

0 Answers0