Question

I am trying to make an application with questions and each question has 4 answer options (multiple choice), and I used 4 radio buttons for the 4 answers. is there a way to join them together in a radiogroup or should I handle each radio button individually? a radio group has just 3 radio buttons, can I increase the radio buttons to more than three? if yes then how?

Was it helpful?

Solution

You can get each RadioButton from code like :

RadioGroup rg = (RadioGroup )findViewById(R.id.radio_group);

RadioButton r1 = (RadioButton) rg.getChildAt(0);
RadioButton r2 = (RadioButton) rg.getChildAt(1);
RadioButton r3 = (RadioButton) rg.getChildAt(2);
RadioButton r4 = (RadioButton) rg.getChildAt(3);

Yes you can add more then 3 radio button in Radio Group

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

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

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

            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton" />

            <RadioButton
                android:id="@+id/radio3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton" />
        </RadioGroup>

OTHER TIPS

You must add the radio buttons to a RadioGroup and then the RadioGroup to the layout.

final RadioButton[] rb = new RadioButton[4];
    RadioGroup rg = new RadioGroup(this); //create the RadioGroup
    rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
    for(int i=0; i<4; i++){
        rb[i]  = new RadioButton(this);
        rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
        rb[i].setText("Test");
    }
    ll.addView(rg);//you add the whole RadioGroup to the layout
    ll.addView(submit); 

EDIT:

or you can just define radiogroup in your xml:

<TableRow>
    <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/radiobuttons">
     </RadioGroup>
</TableRow>

and then add one additional button to it programatically:

 RadioGroup rg = (RadioGroup) findViewById(R.id.radiobuttons);//not this RadioGroup rg = new RadioGroup(this);
 rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
    for(int i=0; i<4; i++)
    {
        rb[i]  = new RadioButton(this);
        rg.addView(rb[i]); 
        rb[i].setText("Test");
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top