I've been trying the solution here to simulate a radio group that is two columns wide in a table layout and has 7 entries. After adding the radio buttons to the groups dynamically, I have 4 in one RadioGroup column and 3 in the other RadioGroup column.
Unfortunately, my code is not working. After adding radio buttons dynamically, the first group of radio buttons seem to be acting independently => column one (radiogroup1) can select all of the radio buttons. Column 2 (radiogroup2) you can only select one of the radio buttons at a time.
Here is the code (no comments please about the Global Vars, this is legacy code I'm modifying):
/****Modified to use two radio groups that act like one radio group ***/
private void setupRadioButtonAnswers() {
ArrayList<HolderAnswer> listAnswers = GlobalVars.questionHolders[GlobalVars.arrayRowNumber].getListAnswers();
int NumberInColumns = (int)Math.floor(listAnswers.size()/NUMBER_OF_COLUMNS);
//make higher number of answers on the right
if (listAnswers.size()% NUMBER_OF_COLUMNS > 0) NumberInColumns++;
int count = 0;
for (HolderAnswer answer : listAnswers) {
if (count==NumberInColumns && RadioGroup1) {
RadioGroup1 = false;
count = 0;
}
final AssistekRadioButton button = new AssistekRadioButton(this);
button.setTag(answer.getRecID());
button.setId(GlobalVars.getLatestId());
button.setTextColor(Color.BLACK);
GlobalVars.setupText(con, button, answer.getTextID());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button.setEnabled(false);
handlerReenableView.sendEmptyMessageDelayed(button.getId(), 1000);
button.setChecked(true);
if (RadioGroup1) {
radioGroup.check(button.getId());
//radioGroup2.clearCheck();
}
else {
radioGroup2.check(button.getId());
//radioGroup.clearCheck();
}
}
});
if (answer.getOption(optOther) != null) {
button.setAllowOtherET(true, answer);
} else if (answer.getOption(optOtherET) != null) {
button.setAllowOtherET(true, answer);
} else {
button.setAllowOtherET(false, null);
}
if (answer.getOption(optOtherET2) != null) {
button.setAllowOtherET2(true, answer);
} else {
button.setAllowOtherET2(false, null);
}
if (answer.getOption(optOtherDP) != null) {
button.setAllowOtherDP(true, answer);
} else {
button.setAllowOtherDP(false, null);
}
if (RadioGroup1) {
radioGroup.addView(button);
}
else {
radioGroup2.addView(button);
}
count++;
}
listener1 = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1) {
radioGroup2.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw an exception
radioGroup2.clearCheck(); // clear the second RadioGroup!
radioGroup2.setOnCheckedChangeListener(listener2); //reset the listener
for (int i = 0; i < radioGroup.getChildCount(); i++) {
if (AssistekRadioButton.class.isAssignableFrom(radioGroup.getChildAt(i).getClass())) {
((AssistekRadioButton)radioGroup.getChildAt(i)).setChecked(false);
}
}
}
else {
for (int i = 0; i < radioGroup.getChildCount(); i++) {
if (AssistekRadioButton.class.isAssignableFrom(radioGroup.getChildAt(i).getClass()) && radioGroup.getChildAt(i).getId() != checkedId) {
((AssistekRadioButton)radioGroup.getChildAt(i)).setChecked(false);
}
}
AssistekRadioButton checkedRadioButton = (AssistekRadioButton)radioGroup.findViewById(checkedId);
checkedRadioButton.setChecked(true);
}
}
};
listener2 = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1) {
radioGroup.setOnCheckedChangeListener(null);
radioGroup.clearCheck();
radioGroup.setOnCheckedChangeListener(listener1);
for (int i = 0; i < radioGroup2.getChildCount(); i++) {
if (AssistekRadioButton.class.isAssignableFrom(radioGroup2.getChildAt(i).getClass())) {
((AssistekRadioButton)radioGroup2.getChildAt(i)).setChecked(false);
}
}
}
else {
for (int i = 0; i < radioGroup2.getChildCount(); i++) {
if (AssistekRadioButton.class.isAssignableFrom(radioGroup2.getChildAt(i).getClass()) && radioGroup2.getChildAt(i).getId() != checkedId) {
((AssistekRadioButton)radioGroup2.getChildAt(i)).setChecked(false);
}
}
AssistekRadioButton checkedRadioButton = (AssistekRadioButton)radioGroup2.findViewById(checkedId);
checkedRadioButton.setChecked(true);
}
}
};
radioGroup.setOnCheckedChangeListener(listener1);
radioGroup2.setOnCheckedChangeListener(listener2);
}
and the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/transitionForm"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/backgroundcolor"
android:orientation="horizontal" >
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/assistekborders" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/assistek_pro_borderwidgets" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/assistek_pro_questionlabels" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/assistek_pro_practicesessionwidgets" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/assistek_pro_progress" />
<ScrollView
android:id="@+id/svPRORGWidgets"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ImageBottom"
android:layout_below="@+id/lblSubQuestionText"
android:layout_marginBottom="30px"
android:layout_marginRight="50px"
android:layout_marginTop="30px"
android:layout_toRightOf="@+id/lblQuestionNumber"
android:descendantFocusability="beforeDescendants" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
-->
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="fill_parent"
android:layout_column="1"
android:layout_height="wrap_content" >
</RadioGroup>
<LinearLayout
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_column="2"
android:orientation="horizontal">
</LinearLayout>
<RadioGroup
android:id="@+id/radioGroup2"
android:layout_width="fill_parent"
android:layout_column="3"
android:layout_height="wrap_content" >
</RadioGroup>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
Does anything jump out at anyone?