2

I am trying to Create 2 rows of Radio buttons like the picture below. But my radio group is not working when I directly put it above the LinearLayout.

I am new to android development. So if there are better alternatives. They are welcome

enter image description here

My code

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout2"
        android:background="@android:color/holo_blue_dark">
        <RadioButton
            android:text="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton33" />
        <RadioButton
            android:text="2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton34" />
        <RadioButton
            android:text="3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton35" />
        <RadioButton
            android:text="4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton36" />
        <RadioButton
            android:text="5"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton37" />
        <RadioButton
            android:text="6"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton38" />
        <RadioButton
            android:text="7"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/radioButton39"
            android:layout_marginRight="1.0dp" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout3"
        android:background="@android:color/holo_blue_dark">
        <RadioButton
            android:text="8"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton40" />
        <RadioButton
            android:text="9"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton41" />
        <RadioButton
            android:text="10"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton42" />
        <RadioButton
            android:text="11"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton43" />
        <RadioButton
            android:text="12"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton44" />
        <RadioButton
            android:text="13"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton45" />
        <RadioButton
            android:text="14"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton46" />
    </LinearLayout>
</RadioGroup>

Thanks

Pepernoot
  • 3,409
  • 3
  • 21
  • 46
  • 2
    root layout of Radio Button should be RadioGroup otherwise it will work independently. – D.J Oct 18 '16 at 10:13
  • you dont need to use linear layout...specify android:orientation within radio group...visit this http://stackoverflow.com/questions/15005551/androidplacing-the-radio-buttons-horizontally – H Raval Oct 18 '16 at 10:14
  • Check this http://stackoverflow.com/questions/17258573/multiple-row-radio-buttons-in-android – vidulaJ Oct 18 '16 at 10:15
  • vindulaJ, I used this http://stackoverflow.com/a/17258637/6720987 It's from the link you gave me but it doesn't work – Pepernoot Oct 18 '16 at 10:18
  • No. I used my own solution because I was too lazy to find all the id's ;p – Pepernoot Oct 19 '16 at 09:28

2 Answers2

1

You can create 2 Radio Groups and handle everything from the RadioButtonCliked Event like that example

public void RadioButtonClicked(View view) {
    boolean checked = ((RadioButton) view).isChecked();

    switch(view.getId()) {
        case R.id.radioButton33: {  
            NameRadioGroup2.clearCheck();
            NameRadioGroup1.check(view.getId());
            break;
        }

        case R.id.radioButton40: {
            NameRadioGroup1.clearCheck();
            NameRadioGroup2.check(view.getId());          
            break;
        }
        {here implement cases for other radio buttons}
}


or

public void RadioButtonClicked(View view) {
    boolean checked = ((RadioButton) view).isChecked();
    if(ButtonFromRadioGroupOne(view.getId())
    {
       NameRadioGroup2.clearCheck();
       NameRadioGroup1.check(view.getId());
    }
    else
    {
       NameRadioGroup1.clearCheck();
       NameRadioGroup2.check(view.getId());
    }
}

private bool ButtonFromRadioGroupOne(int id)
{
    return id == R.id.radioButton33 || id == R.id.radioButton34 || id == R.id.radioButton35 || id == R.id.radioButton36 || id == R.id.radioButton37 || id == R.id.radioButton38 || id == R.id.radioButton39;
}

or you can have 2 RadioButtonClicked Events for each radio group.

public void RadioButtonClicked1(View view) {
    RadioButtonClicked(NameRadioGroup1, NameRadioGroup2, view);
}

public void RadioButtonClicked2(View view) {
    RadioButtonClicked(NameRadioGroup2, NameRadioGroup1, view);
}

public void RadioButtonClicked(RadioGroup selected, RadioGroup cleared, View view) {
    boolean checked = ((RadioButton) view).isChecked();
    cleared.clearCheck();
    selected.check(view.getId());
}     
gmetax
  • 3,853
  • 2
  • 31
  • 45
0

I fixed it

First I created an extension Method to get te child from a View

static class Extention
    {
        public static List<T> Childs<T>(this ViewGroup view) where T : View
        {
            List<T> childs = new List<T>();
            for (int i = 0; i < view.ChildCount; i++)
            {
                var selectedView = view.GetChildAt(i) as T;
                if (view != null)
                {
                    childs.Add(selectedView);
                }
            }
            return childs;
        }
    }

I added this code in my main

LinearLayout Layout2 = FindViewById<LinearLayout>(Resource.Id.linearLayout2);
            LinearLayout Layout3 = FindViewById<LinearLayout>(Resource.Id.linearLayout3);
            Buttons = Layout2.Childs<RadioButton>();  
            Buttons.AddRange(Layout3.Childs<RadioButton>());
            foreach(RadioButton button in Buttons)
            {
                button.CheckedChange += RadioButton_Check;
            }

And this is my event

 public bool InUse = false;
        private void RadioButton_Check(object sender, CompoundButton.CheckedChangeEventArgs e)
        {
            if (InUse) return;
            InUse = true;
            RadioButton Senderbutton = sender as RadioButton;
            foreach (RadioButton button in Buttons)
            {
                button.Checked = false;
            }
            Senderbutton.Checked = true;
            InUse = false;
        }
Pepernoot
  • 3,409
  • 3
  • 21
  • 46