0

I'm new to android studio and am trying to have a 2 x 2 layout for some radio buttons. I have looked online and they're are similar question so apologies for a bit of a repeat but whatever I do I cannot seem to get mine to work. This is what I have in my xml file.

  <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup">

       <TableRow>

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="@string/text1" />

            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/text2" />
        </TableRow>
        <TableRow>
            <RadioButton
                android:id="@+id/radio3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/text3"/>

            <RadioButton
                android:id="@+id/radio4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/text4"/>
        </TableRow>
    </RadioGroup>

Can anyone explain why that doesn't work? It just lets me select all the buttons.

Dan Murphy
  • 225
  • 1
  • 5
  • 15

1 Answers1

0

Radio Group in itself is a linear layout.

The "orientation" attribute in radio group allows you to align the radio buttons horizontally or vertically (like a linear layout). There is no need for a separate linear layout in your xml.

But, in your case because as you are trying to design them in a grid fashion, you are not able to do the mutual exclusion logic of the Radio group

You can also refere to this for an answer How to group a 3x3 grid of radio buttons?

Ravi
  • 881
  • 1
  • 9
  • 23
  • Thanks for your help. I updated the code to use like the example you linked me and still no luck. Is there a way I can achieve the mutual exclusion this way? – Dan Murphy Oct 31 '17 at 15:43
  • One thing that i can suggest is , you write logic to check/uncheck individual radio buttons based on their checked state.. – Ravi Oct 31 '17 at 15:53
  • sorry I don't get what you mean? I thought the whole point of radio groups is so that only one can be checked at a time, I thought it would automatically do this no? – Dan Murphy Oct 31 '17 at 16:00
  • Yes, the basic functionality of radio group is to handle the mutual exclusion by itsef. But, because you are wanting to put the radio options in a grid manner (which is your custom UI) , the functionality always has to be custom as the radio group dosen't handle it. If you simply put all your options either horizontally/vertically inside the radio group , mutual exclusion is handled by the radio group. – Ravi Oct 31 '17 at 16:04
  • oh I see now thanks very much. Have you any tips on how to do that? Sorry I am a complete beginner – Dan Murphy Oct 31 '17 at 16:10
  • Use radioButton.isChecked() and radioButton.setChecked(true/false) these methods and change the checked state of buttons – Ravi Oct 31 '17 at 17:24