2

I need to add radio button dynamically. A radio button may be 3, 4, 5 or 6 and it would be added horizontally and one row contains maximum 3 radio button.

If there are more than 3 then it would come below of above row of radio button as in grid view. My code for radio button are below but it display all radio button in a single row, means it's hiding the radiobutton.

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
 android:orientation="vertical" >

   <TextView
 android:id="@+id/TextView01"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Choose Your Favorite Actress" >
  </TextView>

 <RadioGroup
android:id="@+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
 </RadioGroup>
 <Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" >
 </Button>
</LinearLayout>

And Java class is:

  @Override
  public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       DisplayRadioButton();
  }


  public void DisplayRadioButton() {
       for(int i=0;i<10;i++) {
           RadioGroup radiogroup = (RadioGroup)findViewById(R.id.RadioGroup01);
           RadioButton rdbtn = new RadioButton(this);
           rdbtn.setId(i);
           rdbtn.setText(text[i]);
           radiogroup.addView(rdbtn);
  }

}
halfer
  • 19,824
  • 17
  • 99
  • 186
SRam
  • 2,832
  • 4
  • 45
  • 72
  • Why dont you take a grid view of radio buttons and just change the adapter according to your need . – Shachillies Jul 18 '12 at 08:03
  • hi deepak.. i never user grid view of radio button, can u please suggest me some links or as well if u can share me the code..i will be highly thakfull of you – SRam Jul 18 '12 at 08:50
  • deepak actually in my project i have a requirment like i have a question and there are some answer based on this with radio button , options are coming dynamically , some question have 4 options and some have 5, or 6 so i need to implement it dynamically..i think u get my point..so now if u can please help me – SRam Jul 18 '12 at 09:13
  • 1
    http://vikaskanani.wordpress.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/ . Please look into the below link and modify according to your need. Remove imageview and use checkboxes only and when next question of checkboxes come just notifydatasetchange of gridview according to no of answers. – Shachillies Jul 18 '12 at 09:54
  • hello deepak , i follow the same as you suggest means i remove image view from existing code and use only radio button for the same , but now i need to set text besides left of each radio button , so how to manage text and radiobutton means if there is long text then it come across the radio button, and it's showing 3 radio button always either if we have long text associated with that radio button ...now i want to automatically adjust radio button according to text besides it..is it possible now? – SRam Jul 18 '12 at 12:19
  • For adding the Text please add the textview in place of Imageview that you removed , if text is long you can set some of teh textview properties like maxcahr or ellipsize etc .. – Shachillies Jul 19 '12 at 09:40

1 Answers1

3

please try in the following way:

1)in your xml remove the RadioGroup. create it by dynamically

RadioGroup radiogroup[];
RadioButton rdbtn[];
LinearLayout linear[]; 

radiogroup = new RadioGroup[9/3];
rdbtn = new RadioButton[9];
linear = new LinearLayout[9/3];

......
int count = 0; // integer flag

for(int i=0;i<9;i++){

  if the value of i is equal to 3 multiple then increase count by 1
         // sett linear[count]'s orientation is horizontal.

         root_layout.addView(linear[count]);
         radiogroup[count] = new RadioGroup(this);
         linear[count].addView(radiogroup[count]);  // add radio group to linear layout

       add radio button to radio group.

          rdbtn[i] = new RadioButton(this);
          rdbtn[i].addView(radiogroup[count]);


}

i hope you get solved. be aware of array index out of bound exception.

your xml may look like:

<LinearLayout
           android:id= rootlayout
           ..... // the child linearlayout
                     .. . radio group
                        ... radio button
 </LinearLayout>   
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182
  • thanks for ur reply ,i need to set radio button horizontally so what shuld i do for setting it horizontolly..? – SRam Jul 18 '12 at 12:15
  • see edited answer, you have to set root linear layout orientation is vertical and you have to create child linear layout by dynamically with horizontal orientation. – M.A.Murali Jul 18 '12 at 12:33
  • PRIOR u was saying that remove radio group now in ur xml u r adding Radio Group and in your java code where u make the instance of radio button using the XML? – SRam Jul 18 '12 at 13:17
  • i think your way is right but can you please explore your code more..coz i m not getting all what you are doing.. – SRam Jul 18 '12 at 13:24
  • @saurabh trivedi, you have to set width is full parent and height is wrap_content for child linear layout and radiogroup, for the radio button you have to set both height and width as wrap_content. note: every thing done through in coding. i am away of my laptop so i could not explore more in coding... – M.A.Murali Jul 18 '12 at 13:39
  • ok murali no problem i will wait for your very nice concept's code..i would be very thankfull if u can provide me code on my email id :saurabh26213@gmail.com ....on the while ill try to do according to your suggestion.. – SRam Jul 18 '12 at 13:43
  • the xml shown in answer is typical one. that is i show you how the xml structure look. – M.A.Murali Jul 18 '12 at 13:43
  • as u said that if the value of i is equal to 3 multiple then increase count by 1 what the case when i is not multiple of 3 ? then i would not jump to next row? – SRam Jul 18 '12 at 13:48
  • yes sure, i will update my answer when i did some code and request a help from you that is,i need to create oscilloscope, for more detail please see http://stackoverflow.com/questions/10882990/how-to-create-oscilloscope-for-audio-on-android – M.A.Murali Jul 18 '12 at 13:59
  • its spectram analyser friend , ill send you code on ur ID but first you should have to help me and if u can send me code on my above given id then sure ill give u my spectram analyser code to you by sure..thanks friends – SRam Jul 18 '12 at 14:10
  • my mail id is murali6060.android@gmail.com. i will update you soon. have you got oscilloscope? please friend i need it. – M.A.Murali Jul 18 '12 at 14:14
  • ok ill wait for ur Code , den same time ill send u Spectram analyser code to u ..dats called android unity..:) thanks friend please do my code as soon as possible .. – SRam Jul 18 '12 at 14:17
  • Hi friend i sent my code to your mail. please check it. and i request send your oscilloscope code to my mail id. i am under high pressure to do oscilloscope. i am beyond the office time to get radio group, please send your code to my mail id. – M.A.Murali Jul 18 '12 at 15:06