Reputation: 141
when I set the radio group to be horizontal, it only has space for 2 radio buttons, and when I set it to vertical I have space for as many as I want but only one per line. What I want to have is 2 radio buttons per line and 5 lines of them, all inside of the same radio group, how can I do this?
This is how I wanted it to be like:
Upvotes: 1
Views: 2222
Reputation: 141
This solution is a bit strange but it will work.
For view you will use:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp"
android:weightSum="2">
<RadioGroup
android:id="@+id/Radio1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RADIO 1" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RADIO 2" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RADIO 3" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RADIO 4" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RADIO 5" />
</RadioGroup>
<RadioGroup
android:id="@+id/Radio2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RADIO 6" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RADIO 7" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RADIO 8" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RADIO 9" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RADIO 10" />
</RadioGroup>
</LinearLayout>
<Button
android:id="@+id/MyButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CHECK"
/>
</LinearLayout>
And activity like this:
public class MainActivity : Activity
{
private RadioGroup _rg1;
private RadioGroup _rg2;
private Button _btn;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
_rg1 = FindViewById<RadioGroup>(Resource.Id.Radio1);
_rg2 = FindViewById<RadioGroup>(Resource.Id.Radio2);
_rg1.ClearCheck();
_rg2.ClearCheck();
_rg1.CheckedChange += OnRg1Change;
_rg2.CheckedChange += OnRg2Change;
_btn = FindViewById<Button>(Resource.Id.MyButton);
_btn.Click += (sender, args) =>
{
var check1 = _rg1.CheckedRadioButtonId;
var check2 = _rg2.CheckedRadioButtonId;
var realCheck = check1 == -1
? check2
: check1;
Toast.MakeText(this, realCheck.ToString(), ToastLength.Long).Show();
};
}
private void OnRg2Change(object sender, RadioGroup.CheckedChangeEventArgs e)
{
if (e.CheckedId == -1) return;
_rg1.CheckedChange -= OnRg1Change;
_rg1.ClearCheck();
_rg1.CheckedChange += OnRg1Change;
}
private void OnRg1Change(object sender, RadioGroup.CheckedChangeEventArgs e)
{
if (e.CheckedId == -1) return;
_rg2.CheckedChange -= OnRg2Change;
_rg2.ClearCheck();
_rg2.CheckedChange += OnRg2Change;
}
}
Upvotes: 2