2

My question is similar to this question and since I was unable to find a satifying answer I would like to put up my question explicitly.

I have a custom adapter which takes an array of strings and has to populate the the list (defined in my ListActivity class) with that array. Now, the layout of each row has a text view and and a radiobutton as shown

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="wrap_content">
<TextView android:text="@+id/word" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:id="@+id/word"
    android:textSize="30px">
</TextView>
<RadioButton
    android:id="@+id/selection"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="4px"
    android:layout_marginRight="10px"
    android:layout_alignParentRight="true"
    android:layout_x="27px"
    android:layout_y="8px">
</RadioButton>

Everything is working fine till this point. I am able to render each row with a radiobutton. What I am looking for is a way in which I can group those radiobuttons together to perform a single selection. This is required for an an application I am making in which has a question with four options as answers. Also please tell me whether this is the right approach for this sort of a problem or not. Thanks in advance

Community
  • 1
  • 1
ric03uec
  • 145
  • 4
  • 12

1 Answers1

0

I could have been more clear as well. Anyhow, your final assembled XML needs to look like this. You could be doing this in code and that is fine as long as what you're building is returned as such. Each RadioButton must fall within the RadioGroup container.

<RadioGroup
            android:id="@+id/rg_configup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            >
            <RadioButton
                android:id="@+id/rb_configup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sales"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"
                >
            </RadioButton>
            <RadioButton
                android:id="@+id/rb_configup2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Regional"
                android:layout_above="@+id/rb_configup1"
                android:layout_alignLeft="@+id/rb_configup1"
                >
            </RadioButton>
            <RadioButton
                android:id="@+id/rb_configup3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Global"
                android:layout_above="@+id/rb_configup2"
                android:layout_alignLeft="@+id/rb_configup1"
                >
            </RadioButton>
            <RadioButton
                android:id="@+id/rb_configup4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ad-hoc"
                android:layout_above="@+id/rb_configup3"
                android:layout_alignLeft="@+id/rb_configup1"
                >
            </RadioButton>
        </RadioGroup>

Does this make sense, or are you trying to do this in something other than XML?

apesa
  • 12,163
  • 6
  • 38
  • 43
  • ihad tried this earlier. This does not group the radiobuttons. I am using the radiobuttons by using (RadioButton) view.findViewById(R.id.selection) in my custom adapter. Using the above code does not group my buttons. I still can select all of them together – ric03uec Mar 28 '11 at 20:42
  • This is what I am using to allow only one of four radio buttons to be selected at any time. It works for me. To group as in place radio buttons on the screen I am using a relativelayout – apesa Mar 28 '11 at 20:43
  • I am using the same deal with an eventlistener and I have four radio buttons on my screen. – apesa Mar 28 '11 at 20:46
  • I again tried to do do that but its still not working, I am still able to select all the options. Can you post a code snippet showing how actually are you using the radiobuttons which you have defined in the code you provided. – ric03uec Mar 29 '11 at 21:42
  • See, it is working fine if I use the radiobutton.settext(text) method to set the text for buttons. But in the adapter, I have a SEPARATE text view and a radio button for each row, and I need to group those radio buttons of different rows together. I hope I made the question more clear now. – ric03uec Mar 30 '11 at 06:06
  • For grouping to work you have to have each RadioButton inside the RadioGroup. If you're using these inside a ListView or similar separate container you may have to use a different Layout – apesa Apr 01 '11 at 00:48
  • Yes. I did a lot of lookup on that only to figure out what you have just said. I will now be using a different layout. – ric03uec Apr 02 '11 at 06:41
  • Apparently there is no way to do what i was trying to and there is a workaround as mentioned here[link](http://stackoverflow.com/questions/2381560/how-to-group-a-3x3-grid-of-radio-buttons) This did solve the issue – ric03uec Apr 03 '11 at 18:10