1

I have created a layout that will be inflated to a container this layout contain radiobutton

Problem: The layout inflated but all the radio button get checked, is this wrong?.

Layout containing radio button to be inflated in the container.

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="23dp"
   android:layout_marginTop="10dp"
   android:orientation="horizontal">
   <RadioButton
    android:id="@+id/radioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>

Layout for the container

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <RadioGroup
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

Code Inflating the child

 LayoutInflater inflater = (LayoutInflater)   getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0 ; i < 6 ; i++){
        LinearLayout child =(LinearLayout) inflater.inflate(R.layout.layout_linear_with_rb,container,false);
        container.addView(child);
    }

Screenshot:RadioButton screenshot containing someview

TABZ
  • 125
  • 12

2 Answers2

1

RadioGroup documentation

The selection is identified by the unique id of the radio button as defined in the XML layout file.

And looking at your codebase, I see that you don't have an unique Id for your RadioButton.

I made a sample project and tried adding RadioButton's dynamically with unique Ids and it works flawlessly.

    RadioGroup container = findViewById(R.id.container);

    for (int i = 0 ; i < 6 ; i++){
        RadioButton radioButton = new RadioButton(this);
        radioButton.setId(i);
        container.addView(radioButton);
    }

There might be a problem of conflicting ids in this case. Maybe an id of 0 is set on some other view. To avoid such confusion I recommend using View.generateViewId() to generate an unique id.

View.generateViewId() is available only from API >= 17.

Edit 1

Please stop using LinearLayout as parent inside your RadioButton layout. A quick fix for you will be changing the RadioButton layout file to

<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

And changing your Java code to

LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0 ; i < 6 ; i++){
    RadioButton radioButton = (RadioButton) inflater.inflate(R.layout.layout_linear_with_rb,container,false);
    radioButton.setId(i);
    container.addView(radioButton);
}
capt.swag
  • 10,335
  • 2
  • 41
  • 41
1

The problem here is the RadioGroup it looks for a RadioButton child only, and because Im using a nested layout that contain the RadioButton the RadioGroup cannot find the RadioButton that is inflated programmatically.

How I solve this problem is with the https://github.com/worker8/RadioGroupPlus this is a tweak RadioGroup that dig deep on nested layout and find the RadioButton inside.

TABZ
  • 125
  • 12