Try this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
<RadioGroup
android:id="@+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
</LinearLayout>
Java code :
private OnCheckedChangeListener listener1 = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1) {
m_group2.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw that stackoverflow exception(like Vladimir Volodin pointed out)
m_group2.clearCheck(); // clear the second RadioGroup!
m_group2.setOnCheckedChangeListener(listener2); //reset the listener
// Log.e("XXX2", "do the work");
int chkId1 = m_group1.getCheckedRadioButtonId();
int chkId2 = m_group2.getCheckedRadioButtonId();
int realCheck = chkId1 == -1 ? chkId2 : chkId1;
RadioButton rb = (RadioButton)findViewById(realCheck);
selected_mode = rb.getText().toString();
Log.i("Selected_Mode", "" + selected_mode);
}
}
};
private OnCheckedChangeListener listener2 = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1) {
m_group1.setOnCheckedChangeListener(null);
m_group1.clearCheck();
m_group1.setOnCheckedChangeListener(listener1);
//Log.e("XXX2", "do the work");
int chkId1 = m_group1.getCheckedRadioButtonId();
int chkId2 = m_group2.getCheckedRadioButtonId();
int realCheck = chkId1 == -1 ? chkId2 : chkId1;
RadioButton rb = (RadioButton)findViewById(realCheck);
selected_mode = rb.getText().toString();
Log.i("Selected_Mode", "" + selected_mode);
}
}
};
call the listener to OnCreate()
m_group1.setOnCheckedChangeListener(listener1);
m_group2.setOnCheckedChangeListener(listener2);