Radiogroup with Two Columns Which Have Ten Radiobuttons

RadioGroup with two columns which have ten RadioButtons

You can simulate that RadioGroup to make it look like you have only one. For example you have rg1 and rg2(RadioGroups with orientation set to vertical(the two columns)). To setup those RadioGroups:

rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
rg2 = (RadioGroup) findViewById(R.id.radioGroup2);
rg1.clearCheck(); // this is so we can start fresh, with no selection on both RadioGroups
rg2.clearCheck();
rg1.setOnCheckedChangeListener(listener1);
rg2.setOnCheckedChangeListener(listener2);

To select only one RadioButton in those RadioGroups the listeners above will be:

private OnCheckedChangeListener listener1 = new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1) {
rg2.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw that stackoverflow exception(like Vladimir Volodin pointed out)
rg2.clearCheck(); // clear the second RadioGroup!
rg2.setOnCheckedChangeListener(listener2); //reset the listener
Log.e("XXX2", "do the work");
}
}
};

private OnCheckedChangeListener listener2 = new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1) {
rg1.setOnCheckedChangeListener(null);
rg1.clearCheck();
rg1.setOnCheckedChangeListener(listener1);
Log.e("XXX2", "do the work");
}
}
};

To get the checked RadioButton from the RadioGroups you could do:

int chkId1 = rg1.getCheckedRadioButtonId();
int chkId2 = rg2.getCheckedRadioButtonId();
int realCheck = chkId1 == -1 ? chkId2 : chkId1;

If you use the check() method of the RadioGroup you have to remember to call clearCheck() on the other Radiogroup.

RadioGroup with two columns in Kotlin

Just clear inside a change listener:

radioGroup1.setOnCheckedChangeListener { _, _ -> 
radioGroup2.clearCheck()
}

How can I have a Radio Group divided in 2 columns and 5 lines,containing 10 Radio Buttons?

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;
}
}

ConstraintLayout, RadioGroup and two columns of RadioButton

Views have to use layout attributes of their direct parent. You can't, for instance, have RadioButtons with layout_constraints, because the direct parent is a RadioGroup and RadioGroup doesn't know how to interpret those attributes.

RadioGroup extends LinearLayout, so the best you can do with a single RadioGroup is a single row or column of RadioButtons. You could have two RadioGroups in your layout and in your java code listen for changes on both.

private RadioGroup mGroup1; // init in onCreate
private RadioGroup mGroup2; // init in onCreate

private OnCheckedChangedListener mCheckListener = new OnCheckedChangedListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// To make it appear as if the two groups are one large group,
// checking something in either should clear the check in the other.
RadioGroup otherGroup = group == mGroup1 ? mGroup2 : mGroup1;
otherGroup.clearCheck();

// do something with checkedId
}
}

RadioGroup with two columns which have ten RadioButtons

You can simulate that RadioGroup to make it look like you have only one. For example you have rg1 and rg2(RadioGroups with orientation set to vertical(the two columns)). To setup those RadioGroups:

rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
rg2 = (RadioGroup) findViewById(R.id.radioGroup2);
rg1.clearCheck(); // this is so we can start fresh, with no selection on both RadioGroups
rg2.clearCheck();
rg1.setOnCheckedChangeListener(listener1);
rg2.setOnCheckedChangeListener(listener2);

To select only one RadioButton in those RadioGroups the listeners above will be:

private OnCheckedChangeListener listener1 = new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1) {
rg2.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw that stackoverflow exception(like Vladimir Volodin pointed out)
rg2.clearCheck(); // clear the second RadioGroup!
rg2.setOnCheckedChangeListener(listener2); //reset the listener
Log.e("XXX2", "do the work");
}
}
};

private OnCheckedChangeListener listener2 = new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1) {
rg1.setOnCheckedChangeListener(null);
rg1.clearCheck();
rg1.setOnCheckedChangeListener(listener1);
Log.e("XXX2", "do the work");
}
}
};

To get the checked RadioButton from the RadioGroups you could do:

int chkId1 = rg1.getCheckedRadioButtonId();
int chkId2 = rg2.getCheckedRadioButtonId();
int realCheck = chkId1 == -1 ? chkId2 : chkId1;

If you use the check() method of the RadioGroup you have to remember to call clearCheck() on the other Radiogroup.

How to group a 3x3 grid of radio buttons?

Actually it's not that hard if you subclass TableLayout like in this example

/**
*
*/
package com.codtech.android.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.TableLayout;
import android.widget.TableRow;

/**
* @author diego
*
*/
public class ToggleButtonGroupTableLayout extends TableLayout implements OnClickListener {

private static final String TAG = "ToggleButtonGroupTableLayout";
private RadioButton activeRadioButton;

/**
* @param context
*/
public ToggleButtonGroupTableLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

/**
* @param context
* @param attrs
*/
public ToggleButtonGroupTableLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

@Override
public void onClick(View v) {
final RadioButton rb = (RadioButton) v;
if ( activeRadioButton != null ) {
activeRadioButton.setChecked(false);
}
rb.setChecked(true);
activeRadioButton = rb;
}

/* (non-Javadoc)
* @see android.widget.TableLayout#addView(android.view.View, int, android.view.ViewGroup.LayoutParams)
*/
@Override
public void addView(View child, int index,
android.view.ViewGroup.LayoutParams params) {
super.addView(child, index, params);
setChildrenOnClickListener((TableRow)child);
}

/* (non-Javadoc)
* @see android.widget.TableLayout#addView(android.view.View, android.view.ViewGroup.LayoutParams)
*/
@Override
public void addView(View child, android.view.ViewGroup.LayoutParams params) {
super.addView(child, params);
setChildrenOnClickListener((TableRow)child);
}

private void setChildrenOnClickListener(TableRow tr) {
final int c = tr.getChildCount();
for (int i=0; i < c; i++) {
final View v = tr.getChildAt(i);
if ( v instanceof RadioButton ) {
v.setOnClickListener(this);
}
}
}

public int getCheckedRadioButtonId() {
if ( activeRadioButton != null ) {
return activeRadioButton.getId();
}

return -1;
}
}

and create a layout like this (of course you need to clean it up but you got the idea)

<?xml version="1.0" encoding="utf-8"?>
<com.codtech.android.view.ToggleButtonGroupTableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/radGroup1">
<TableRow>
<RadioButton android:id="@+id/rad1" android:text="Button1"
android:layout_width="105px" android:layout_height="wrap_content"
android:textSize="13px" />
<RadioButton android:id="@+id/rad2" android:text="Button2"
android:layout_width="105px" android:textSize="13px"
android:layout_height="wrap_content" />
<RadioButton android:id="@+id/rad3" android:text="Button3"
android:layout_width="105px" android:textSize="13px"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<RadioButton android:id="@+id/rad1" android:text="Button1"
android:layout_width="105px" android:layout_height="wrap_content"
android:textSize="13px" />
<RadioButton android:id="@+id/rad2" android:text="Button2"
android:layout_width="105px" android:textSize="13px"
android:layout_height="wrap_content" />
<RadioButton android:id="@+id/rad3" android:text="Button3"
android:layout_width="105px" android:textSize="13px"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<RadioButton android:id="@+id/rad1" android:text="Button1"
android:layout_width="105px" android:layout_height="wrap_content"
android:textSize="13px" />
<RadioButton android:id="@+id/rad2" android:text="Button2"
android:layout_width="105px" android:textSize="13px"
android:layout_height="wrap_content" />
<RadioButton android:id="@+id/rad3" android:text="Button3"
android:layout_width="105px" android:textSize="13px"
android:layout_height="wrap_content" />
</TableRow>
</com.codtech.android.view.ToggleButtonGroupTableLayout>


Related Topics



Leave a reply



Submit