3

I want to give the user four choices, the first and second choices on a row and the third and fourth choices on another row. My problem is when the application starts I can select more than one choice, but I don't want that. This is my xml layout:

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

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rAnswerQuestonChoic1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton"
                android:visibility="invisible" />

            <RadioButton
                android:id="@+id/rAnswerQuestionChoice2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton"
                android:visibility="invisible" />
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rAnswerQuestionChoice3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton"
                android:visibility="invisible" />

            <RadioButton
                android:id="@+id/rAnswerQuestionChoice4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton"
                android:visibility="invisible" />
        </LinearLayout>
    </RadioGroup>

What am I doing wrong?

user
  • 86,916
  • 18
  • 197
  • 190
Totti
  • 665
  • 5
  • 12
  • 26
  • "i can't select more than one choice , but i don't want that " - what do you want? Select two choices, three, four, none? – Simon Jun 23 '12 at 09:56
  • you are using radio group it wont allow you to select more than one radiobutton – Akram Jun 23 '12 at 09:57
  • no no sorry , i don't want to select more than one choice , i will edit the question – Totti Jun 23 '12 at 09:59
  • i mean , i used radio group and i can select more than one choice , where is the wrong – Totti Jun 23 '12 at 10:00

2 Answers2

6

If you place other layouts between the RadioButtons and the parent Radiogroup(like you did with those LinearLayouts) then the mutual exclusion will not work anymore.

To put those RadioButtons in a two rows table you could make your own RadioGroup which places the RadioButtons like you want or you can try to simulate that layout by having two RadioGroups that behave as one(for example, RadioGroup with two columns which have ten RadioButtons).

Community
  • 1
  • 1
user
  • 86,916
  • 18
  • 197
  • 190
  • so what is the solution ? what ever the solution was but i want to keep the first and second choice at one row and the third and fourth choices at another row, thank you for helping – Totti Jun 23 '12 at 10:06
  • just one thing please , i did as the link you gave to me , but i have a problem , please see this piece of code ,the radio buttons doesn't appear vertically http://www.mediafire.com/?t4jo313u9rcy9o8 – Totti Jun 23 '12 at 10:34
  • 1
    @Totti I didn't understand what problems you encounter. The two `RadioGroups` from your link should be put in another layout(like a `LinearLayout` with orientation `horizontal`) to form the two rows table. See this link as an example https://gist.github.com/2977860 – user Jun 23 '12 at 10:50
1

I know its late but I post it for anybody that face this problem in android. if you use RadioGroup in android, you can't put RadioButtons anywhere you want except the exact child of your RadioGroup with no depth of hierarchy. so if you want to put your RadioButtons in a LinearLayout or manage it in a grid, buttons don't act like their belongs to the group (they will stay checked if the user check another RadioButton in the group).

My solution

I wrote a class named RadioGroupCheckListener.java and simply use this line of code to declare a group:

RadioGroupCheckListener.makeGroup(radioButton1, radioButton2, radioButton3, radioButton4);

you can find RadioGroupCheckListener.java in my github RadioGroupCheckListener

import android.widget.CompoundButton;


public class RadioGroupCheckListener implements CompoundButton.OnCheckedChangeListener {

    private CompoundButton[] allies;

    /**
     * public generator - indicate allies
     * @param allies all other buttons in the same RadioGroup
     */
    public RadioGroupCheckListener(CompoundButton... allies){
        this.allies = allies;
    }

    /**
     * listener for a button to turn other allies unchecked when it turn checked
     * @param buttonView change check occur with this button
     * @param isChecked result of changing
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            for (CompoundButton aly : allies) {
                aly.setChecked(false);
            }
        }
    }

    /**
     * inner private function to remove one element from Buttons array
     * @param buttons all the buttons in RadioGroup
     * @param me the index that we want to remove from array
     * @return an array of CompoundButtons except the index of me
     */
    private static CompoundButton[] exceptMe(CompoundButton[] buttons, int me){
        CompoundButton[] result = new CompoundButton[buttons.length-1];
        for(int i=0,j=0;i<buttons.length;i++){
            if(i==me){
                continue;
            }
            result[j]=buttons[i];
            j++;
        }
        return result;
    }

    /**
     * static function to create a RadioGroup
     * if a button turn to checked state all other buttons is group turn unchecked
     * @param buttons the buttons that we want to act like RadioGroup
     */
    public static void makeGroup(CompoundButton... buttons){
        for(int i=0;i<buttons.length;i++){
            buttons[i].setOnCheckedChangeListener(new RadioGroupCheckListener(exceptMe(buttons, i)));
        }
    }
}
Community
  • 1
  • 1
farzinlize
  • 11
  • 1