How do you put dynamically loaded radio buttons into a group?

Radio buttons are supposed to be mutually exclusive. But they need to be in a “radio group.”

According to the docs, “By grouping them together, the system ensures that only one radio button can be selected at a time.”

I made some radio buttons (dynamically loaded) and I made sure to put them in a group. However, they are not mutually exclusive and I can check all of them at once.

How do I fix this?

Here is the code:

public class MainActivity extends AppCompatActivity {

    private LinearLayout candidatesLayout;
    private String[] candidates = {"Smith","Johnson","Williams","Brown","Jones","Garcia","Miller","Davis","Rodriguez","Martinez"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        candidatesLayout = (LinearLayout) findViewById(R.id.candidatesLayout);

        final int N = candidates.length; // total number of textviews to add
        final TextView[] myTextViews = new TextView[N]; // create an empty array;

        for (int i = 0; i < N; i++) {
            // create a new textview
            final TextView rowTextView = new TextView(this); //rowTextView is persons name
            //create horizonal parent for radio buttons
            LinearLayout radioButtons = new LinearLayout(this);
            LinearLayout numbers = new LinearLayout(this);
            radioButtons.setOrientation(LinearLayout.HORIZONTAL);
            //create new radio buttons
            final RadioGroup radioGroup = new RadioGroup(this);
            RadioButton button;
            for(int ii = 0; ii < N; ii++) {
                button = new RadioButton(this); //I should not be able to click more than one of these!
                radioButtons.addView(button);
            }
            candidatesLayout.addView(rowTextView);
            candidatesLayout.addView(radioButtons);
          }

    }
}

I am guessing that they are not really in the group that I think they are in.

Answer

You have created RadioGroup, but didn’t add it to the layout. So, replace radio buttons adding with the following snippet:

 for (int i = 0; i < N; i++) {
    // create a new textview
    final TextView rowTextView = new TextView(this); //rowTextView is persons name
    //create horizonal parent for radio buttons
    LinearLayout radioButtons = new LinearLayout(this);
    LinearLayout numbers = new LinearLayout(this);
    radioButtons.setOrientation(LinearLayout.HORIZONTAL);
    //create new radio buttons
    final RadioGroup radioGroup = new RadioGroup(this);
    RadioButton button;
    for(int ii = 0; ii < N; ii++) {
        button = new RadioButton(this); 
        radioGroup.addView(button);  // <-- Add to Group
    }
    candidatesLayout.addView(rowTextView);
    candidatesLayout.addView(radioGroup);  // Add Group itself
  }

Leave a Reply

Your email address will not be published. Required fields are marked *