RadioGroup with two columns which have ten RadioButtons

You can simulate that RadioGroup to make it look like you have only one. For example you have rg1 and rg2(RadioGroups with orientation set to vertical(the two columns)). To setup those RadioGroups:

rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
rg2 = (RadioGroup) findViewById(R.id.radioGroup2);
rg1.clearCheck(); // this is so we can start fresh, with no selection on both RadioGroups
rg2.clearCheck();
rg1.setOnCheckedChangeListener(listener1);
rg2.setOnCheckedChangeListener(listener2);

To select only one RadioButton in those RadioGroups the listeners above will be:

private OnCheckedChangeListener listener1 = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId != -1) {
                rg2.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw that stackoverflow exception(like Vladimir Volodin pointed out)
                rg2.clearCheck(); // clear the second RadioGroup!
                rg2.setOnCheckedChangeListener(listener2); //reset the listener
                Log.e("XXX2", "do the work");
            }
        }
    };

    private OnCheckedChangeListener listener2 = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId != -1) {
                rg1.setOnCheckedChangeListener(null);
                rg1.clearCheck();
                rg1.setOnCheckedChangeListener(listener1);
                Log.e("XXX2", "do the work");
            }
        }
    };

To get the checked RadioButton from the RadioGroups you could do:

int chkId1 = rg1.getCheckedRadioButtonId();
int chkId2 = rg2.getCheckedRadioButtonId();
int realCheck = chkId1 == -1 ? chkId2 : chkId1;

If you use the check() method of the RadioGroup you have to remember to call clearCheck() on the other Radiogroup.


If the layout is not complicated, best way is to use Single RelativeLayout instead of multiple Linear Layouts.

Below is the code with 2 rows. First row has 3 columns. Second row one column.

            <RadioGroup
                android:id="@+id/radio_group"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="4dp"
                android:layout_marginTop="4dp"
                android:orientation="vertical">

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

                    <android.support.v7.widget.AppCompatRadioButton
                        android:id="@+id/r1c1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentTop="true"
                        android:layout_marginRight="8dp"
                        android:gravity="center"
                        android:text="Row 1 Column1" />

                    <android.support.v7.widget.AppCompatRadioButton
                        android:id="@+id/r2c1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/r1c1"
                        android:layout_gravity="left|center_vertical"
                        android:layout_marginRight="8dp"
                        android:layout_weight="1"
                        android:gravity="left|center_vertical"
                        android:text="Row 2 Column 1" />

                    <android.support.v7.widget.AppCompatRadioButton
                        android:id="@+id/r1c2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="8dp"
                        android:layout_toRightOf="@id/r1c1"
                        android:gravity="center"
                        android:text="Row 1 Column 2"/>

                    <android.support.v7.widget.AppCompatRadioButton
                        android:id="@+id/r1c3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="8dp"
                        android:layout_toRightOf="@id/r1c2"
                        android:gravity="center"
                        android:text="Row 1 Column 3" />
                </RelativeLayout>
            </RadioGroup>

The RadioGroup is extended from LinearLayout.

the linearlayout can not do it, so RadioGroup can not do it.

Why not implement it self.

Use RelativeLayout to layout the child view. And record the state of the child view. use setLevel to control the states.

Good luck for you!.


Make 2 RadioGroup in the xml file using LinearLayout each having 5 RadioButton and using layout_weight property place them side by side on the screen. Then create listener for these radio groups as shown below:

rg1 = (RadioGroup) findViewById(R.id.radiogroup1);
rg2 = (RadioGroup) findViewById(R.id.radiogroup2);
rg1.clearCheck();//this is so we can start fresh, with no selection on both RadioGroups
rg2.clearCheck();
rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            if (checkedId != -1) {
                fun2();
            }
        }
    });

    rg2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            if (checkedId != -1) {
                fun1();
            }
        }
    });

And fun1() & fun2() will be defined as shown below:

public void fun1() {
     rg1.setOnCheckedChangeListener(null);
     rg1.clearCheck();
     rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            fun2();
            Log.v("Inside fun1","fun2");
        }
    });
}

public void fun2() {
     rg2.setOnCheckedChangeListener(null);
     rg2.clearCheck();
     rg2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            fun1();
            Log.v("Inside fun2","fun1");

        }
    });
}