1

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?

Community
  • 1
  • 1
alphaJA
  • 31
  • 1
  • 6

1 Answers1

0

You can use ListView instead of GridView.enter image description here

For help please refer to my Android Application blog in

http://amitandroid.blogspot.in/2013/03/android-listview-with-radiogroup.html

Hope this blog will help you lot.

Many Thanks,

Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
  • Thanks for the response. Will this make it so all of these radio buttons are in one RadioGroup?If so, can you elaborate on how you got this? – alphaJA Mar 05 '13 at 06:04
  • You can refer to the link that i posted later. In that link(Android Blog) i wrote the Steps as well as posted complete Source code at bottom. – Amit Gupta Mar 05 '13 at 06:23
  • I tried running the source code, but Eclipse tells me "Unable to resolve target 'Google Inc.:Google APIs:8'" ...I'm guessing our development environment are too different? I'm still new to Eclipse/Android Programming, so I'm not sure why it's not working. Also, it seems like what you have here has one RadioGroup per row, but I want to have one RadioGroup for the entire grid. Is that still possible with ListView? – alphaJA Mar 05 '13 at 15:42
  • I know why are you getting this error. I choose google Api for compiling. So you need to Craete a AVD that should be Google Api from List of AVD. Then run my Source code in that google api AVD, it should run. As you asked that RadioGroup for entire GridView is not posssible b'coz Ita may be either Horizontal or vertical way. Not both the way. In ListView every item having one horizontal RadioGroup. So you can easily identify which row and which RadioButton been checked. Thanks. – Amit Gupta Mar 06 '13 at 02:20
  • Hmm thanks for the help, but unfortunately that is not what I was looking for...I would like to have an entire grid be one RadioGroup. I just find it weird that there isn't a simple way of accomplishing this since it is not an obscure way of laying out radio buttons – alphaJA Mar 06 '13 at 02:48