How to Group a 3X3 Grid of Radio Buttons

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>

Arrange12 RadioButtons in a 4*3 grid

See: How to group a 3x3 grid of radio buttons?

Maybe this could solve your problem, otherwise I wouldn't recommend RadioButtons. Customized Buttons "could" also work in this situation...

Android: Radio button alignment 2 x 2 in one radiogroup

Use LinearLayout within Radiogroup Like this:

<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/radioGroup">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"

android:layout_marginLeft="5dp"/>

<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"

android:layout_marginRight="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"

android:layout_marginLeft="5dp"/>

<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"

android:layout_marginRight="5dp"/>
</LinearLayout>
</RadioGroup>

how can we arrange radio group's button as we want. I want 2 radio buttons in a row and other two radio buttons in next row.

You can simply make each two RadioButtons in one LinearLayoutwhich has an Horizontalorientation. Or you can use GridLayout.



Related Topics



Leave a reply



Submit