How to add radiobuttons to radio group in 3*10 grid layout dynamically

1k views Asked by At

I am trying to add radio buttons to radio groups with table layout when I started the code I am getting some error. Here is my code

    RadioButton[] boxes = new RadioButton[30];
    RadioGroup rg = (RadioGroup) findViewById(R.id.radiogroup);

    for (int i = 0; i < 30; i++) {
        boxes[i] = (RadioButton) findViewById(idArray[i]);
    }

    for (int i = 0; i < 30; i++) {
        rg.addView(boxes[i]);
    }

Xml code is here`

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="11-12(T1)"
                android:id="@+id/radioButton0"
                android:layout_column="9"
                android:checked="false" />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="11-12(T2)"
                android:id="@+id/radioButton10"
                android:checked="false"
                android:layout_marginLeft="40dp" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="11-12(T3)"
                android:id="@+id/radioButton20"
                android:layout_column="11"
                android:checked="false"
                android:layout_marginLeft="40dp" />
        </TableRow>
     </TableLayout>
</RadioGroup>

I didnt post whole xml code because there are 10 table row like that each table row has 3 radio butttons so ı have 30 radio button in 3*10 grid. In java code ı get error like that " The specified child already has a parent. You must call removeView() on the child's parent first."

3

There are 3 answers

0
Sadiq Md Asif On

add the radio buttons to a RadioGroup and then the RadioGroup to the layout. And finally use this to show!

for (int i = 0; i < 10; i++) {

            for (int j = 0; j < 3; j++) {
                radioGroup[j].removeView(radioButton[i]); //now the RadioButtons are in the RadioGroup
            }
            rowLayout.removeView(radioGroup[j]); // now the RadiouGroup are in the row
        }
0
Tzlil Gavra On

As stated in the logs, the radio buttons already have a parent. You added them to the Table Row in the xml what means you can't add them to another parent(your radio group). Also, try in you coding to reuse code you already wrote. it will be more readable, instead of using the same lines in xml. For instance, consider using "include" tag.

maybe something that will help. Because i did just this exactly - radio buttons in table layout. this is a custom layout. just use it to add whatever radio button you want:

the code for the view is in the link: https://github.com/Gavras/MultiLineRadioGroup/blob/master/app/src/main/java/com/whygraphics/multilineradiogroup/MultiLineRadioGroup.java

make sure to also add these:

values/attrs.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="multi_line_radio_group">
        <attr name="max_in_row" format="integer" />
        <attr name="radio_buttons" format="reference" />
        <attr name="default_button" format="string" />
    </declare-styleable>
</resources>

R.layout.table_layout:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/table_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="*" />

R.layout.table_row:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/table_row"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

R.layout.radio_button:(you can change the text size here)

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radio_button"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="@dimen/radio_button_text_size" />

Example using this layout from xml:

<?xml version="1.0" encoding="utf-8"?>
<[package].MultiLineRadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:multi_line_radio_group="http://schemas.android.com/apk/res-auto"
    android:id="@+id/multi_line_radio_group"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    multi_line_radio_group:max_in_row="3"
    multi_line_radio_group:radio_buttons="@array/radio_buttons" />

from the xml you can add res/values/arrays.xml with your desired strings or add from the code using addButtons() method.

0
Dimple Dobariya On

It's not working because of TableLayout inside RadioGroup. All RadioButtons are not grouped together because of TableLayout between them.

RadioButton should be the direct child of RadioGroup, Otherwise grouping does not work.