Reputation: 2344
I have two radio button groups in a view for a dialog box for my user to select the display color they want. I created two groups because of size constraints. Because the two groups do not remain mutually exclusive when separated I need to check for a change from group1 to group2 and clear the selection from the other group. I did this by adding an onCheckedChangeListener() to each group as follows:
RadioGroup rGroup1 = (RadioGroup)currentDialog.findViewById(R.id.rgColors1);
rGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId != -1)
{
RadioGroup rGroup2 = (RadioGroup) currentDialog.findViewById(R.id.rgColors2);
rGroup2.clearCheck();
}
}
});
RadioGroup rGroup2 = (RadioGroup) currentDialog.findViewById(R.id.rgColors2);
rGroup2.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId != -1)
{
RadioGroup rGroup1 = (RadioGroup) currentDialog.findViewById(R.id.rgColors1);
rGroup1.clearCheck();
}
}
});
And my Dialog layout looks like the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/colorDialogLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:minWidth="300dp"
android:orientation="vertical">
<TableLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tableLayout"
android:padding="5dp">
<TableRow android:gravity="center">
<RadioGroup android:id="@+id/rgColors1">
<RadioButton android:id="@+id/black_radio"
android:text="@string/option_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/blue_radio"
android:text="@string/option_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/green_radio"
android:text="@string/option_green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/orange_radio"
android:text="@string/option_orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/pink_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/option_pink" />
</RadioGroup>
<RadioGroup android:id="@+id/rgColors2">
<RadioButton android:id="@+id/purple_radio"
android:text="@string/option_purple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/red_radio"
android:text="@string/option_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/teal_radio"
android:text="@string/option_teal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/yellow_radio"
android:text="@string/option_yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
</TableRow>
</TableLayout>
<Button android:id="@+id/setBackColorButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/button_set_back_color"/>
When a radiobutton is selected from either group then the group it is not part of is cleared. The issue I am having is that the radio button that was selected does not show as checked and must be selected a second time. My suspicion is that the clearCheck() is running for both radiogroups but I am not sure why or how to prevent this behavior. Any help would be much appreciated.
Upvotes: 0
Views: 3257
Reputation: 2344
In the end I had to create an onclick() event for each radio button. The reason I think my first approach does not work is because the onselectchange() event is fired when setting the selection to none. In testing various methods of using this method of setting the second group to none resulted in needing to perform the selection a second time or an infinite loop of the two groups setting each other to none. I believe in the case of needing to perform the selection a second time is because the procedure does not return control to the calling procedure to finish the selection. I am a little green to Android so I could be completely off base. In the end however the separate onclick() event does work.
Upvotes: 2