I would like to lay out a couple dozen radio buttons in a GridView. Right now, I have accomplished this by creating a custom Adapter and overriding the getView function(and then calling setAdapter on the GridView). The only issue is, I am having trouble inclosing these radio buttons in a RadioGroup so I can enforce single selection of button.
I have looked at various stackoverflow posts(such as this one: Radio Group implementation on Grid View in Android), but I have yet to find a straightforward way of getting this done. I have tried throwing in the RadioGroup tag around the GridView tag in my main XML, but that did not work.
Here's some of my related code so far:
private class RadioGridAdapter extends BaseAdapter{
private Context mContext;
RadioGridAdapter(Context c) {
mContext = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if(convertView == null) {
gridView = inflater.inflate(R.layout.cell, null);
//set value into Button
RadioButton ButtonView = (RadioButton) gridView.findViewById(R.id.interval_button);
ButtonView.setText(intervals[position]);
} else {
gridView = (View) convertView;
}
return gridView;
}
}
cell.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="@+id/interval_button"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RadioButton>
</LinearLayout>
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<GridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="20dp"
android:numColumns="auto_fit"
android:paddingLeft="15sp"
android:paddingRight="15sp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
</LinearLayout>
Thanks!
UPDATE: According to this post(radio button multiple rows) and also this post(How to group a 3x3 grid of radio buttons?), there isn't a simple way of doing this without extending the RadioGroup class myself to have a capability of wrapping radio buttons in a grid format.. Does anyone know of an open source project that uses RaioGroup in this way?