1

I have sucessfully made and used RadioGroup's before in xml, but each time there was a set of radio buttons in succession, all within the same LinearLayout. Now I wish to define a set of radio buttons to be part of a group, but they are not in the same layout. My start and end code for the group is:

START:

<RadioGroup    android:id="@+id/radioGroup1" >

END:

</RadioGroup>

If I place this around each button individually, then it compiles, but the buttons don't act as radio buttons (i.e. activating one did not de-activate the others). If I try to place the "start" before any of the buttons and put the "end" after the last of them, then I get compilation errors.

4 Answers 4

3

Store the RadioButtons in an array. Instead of grouping them in a RadioGroup, you have to enable/disable them yourself. (un-tested so no copying/pasting )

declare these variables

private ArrayList<RadioButton> mGroup = new ArrayList<RadioGroup>();
private CompoundButton.OnCheckedChangeListener mListener = new CompoundButton.OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        for(RadioButton btn : mGroup)
            btn.setChecked(false);

        buttonView.setChecked(true);
    }
}

Somewhere in your activity:

mGroup.add(your radiobuttons);    // e.g. (RadioButton)findViewById(R.id.radio_button1);
mGroup.add(another radiobutton);

for(RadioButton btn : mGroup)
   btn.setOnCheckedChangeListener(mListener)

Maybe you have to invalidate your Buttons after checking/unchecking them, to cause a redraw

2

Unfortunately the SDK does not directly support doing this, you would need to create a custom "group" to manager the buttons.

All the mutual exclusion logic for each RadioButton is managed by RadioGroup, so all the buttons have to be added to the same group. RadioGroup is a subclass of LinearLayout, so this inherently means they also need to all be in the same layout. RadioGroup manages each button's checked status by iterating through its children, which is why the two can't be divorced.

Here is a link to the RadioGroup source code as a starting point, you could create a custom Manager that uses the same logic here to manage the status of which button is checked in order to separate them from their layout status. RadioGroup basically just registers itself as an OnCheckedChangeListener for each child that is a RadioButton and then controls the check status of all the buttons based on user events.

HTH

1

Here's a refined and tested version of the first part of Entreco's code:

    final ArrayList<RadioButton> mGroup = new ArrayList<RadioButton>();
    CompoundButton.OnCheckedChangeListener mListener = new CompoundButton.OnCheckedChangeListener() 
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            for(RadioButton btn : mGroup) {
                if (buttonView.getId() != btn.getId() && isChecked) {
                    btn.setChecked(false);
                }
            }
        }
    };

It will uncheck only the other buttons and only if the state of the clicked button changed from unchecked to checked.

1

@Entreco answer can cause recursive call to onCheckedChanged which is fixed by @Twilite answer.

But @Twilite answer also has a problem and that's the id of views without explicit id may not be unique. If that happen, you may see either multiple selected radio buttons or none of them being checked. So, it's safer to either set a unique id or a unique tag for each one:

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

        if (isChecked) {

            for (RadioButton btn : mGroup)
                if (!btn.getTag().equals(compoundButton.getTag()))
                    btn.setChecked(false);

        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.