1

I am using RadioGroup, added RadioButton rdbut to RadioGroup rdgrp like rdgrp.addView(rdbut).

   for(int j=0;j<3;j++)
   {
         RadioGroup rdgrp = new RadioGroup;
         for(int i=0;i<=10;i++)
         {
             RadioButton rdbut = new RadioButton(this);
             rdbut.setText("RadioButtion"+i);
             rdbut.setId(i);
             rdbut.setTag("somename");
             rdgrp.addView(rdbut);
         }
    }    

the above code shows how I initialize the radiogroup and radio button. after I run the this code, in emulator/mobile , i am able to check 2 radio buttons at a time.

What could be the problem?

Jamie Hutton
  • 260
  • 3
  • 13
Raju
  • 737
  • 2
  • 11
  • 21
  • you are creating 3 group of 11 RadioButtons (is this ok?), now there should appear all radio buttons, where you can select 3 radio buttons at a time(one radio from each group). – Pankaj Kumar Nov 25 '11 at 05:26
  • how do you add `rdgrp` to the parent view. and the constructor of `RadioGroup` is also odd (seems typo while copy paste). – Adil Soomro Nov 25 '11 at 05:27
  • In every rationgroup i am able to select two radiobuttons. it means 6 radio buttons, but can't more two in every radiogroup. – Raju Nov 25 '11 at 05:33
  • @Raju i think your goal is to add Radion Button in Radio Group and make use to select only one RadioButton Can be select at time by User from One Group of Radio Button Am i right? – Herry Nov 25 '11 at 06:01

3 Answers3

1

Change your code like this.

  RadioGroup rdgrp[] = new RadioGroup[3];

  For(int j=0;j<3;j++)
   {
         RadioButton rdbut[] = new RadioButton[10];
         For(int i=0;i<=10;i++)
         {

             rdbut[i].setText("RadioButtion"+i);
             rdbut[i].setId(j*100+i);
             rdbut[i].setTag("somename");
             rdgrp[j].addView(rdbut[i]);
         }
    } 
Ramesh Akula
  • 5,720
  • 4
  • 43
  • 67
  • rdbut[] is an array of RadioButton and u r not initializing it anywhere with RadioButton intances.u can edit it. – Android Killer Jul 02 '13 at 06:29
  • @Ramesh Akula Can you tell me what's the problem with the above approach mentioned in the problem statement ? I didn't understood what's wrong in first way. – manish reddy Jan 03 '20 at 12:56
0

You have created three different Radio Group. You can select only one radio button from a single group.So from three groups you can select three buttons But there is no inter-group relationship. You can select radio buttons from different group simultaneously. In your case you can select three buttons at max.

Thanks Sunil

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
0

Use Some thing like this xml design in user layout file.

   <TableLayout
            android:id="@+id/tbl_layoutchoice"
            style="@style/InfoTableView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dip" >

            <RadioGroup
                android:id="@+id/SelectLayout_Group"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </RadioGroup>
 </TableLayout>

and For Use this RadioGroup in Activity 's OnCreate() Method and findView like this

 mRadioGroup = (RadioGroup) this.findViewById(R.id.SelectLayout_Group);

and Then Use Below Code With Your Require Change To Add Radio Button in One RadioGroup.Use Also Below Require Declaration for Create Radio Button Dynamically.

     ArrayList<String> layoutlist = new ArrayList<String>(3);
     int index = -1;
     LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT);


   for (String layout : layoutlist) {
        RadioButton r = new RadioButton(this);
        index++;
        r.setText(layout);
        r.setId(index);
        r.setLayoutParams(lp);
        r.setTextAppearance(this, R.style.TextBase);


        mRadioGroup.addView(r);


    }

So don't forget to add your String Value in layoutlist before for loop .and R.style is some Require Style for Text Show in RadioButton.

Herry
  • 7,037
  • 7
  • 50
  • 80