Creating RadioGroup programmatically

this The specified child already has a parent. You must call removeView() on the child's parent first. because you are adding child( radio group ) to the parent layout multiple times.

try like this

private void createRadioButton() {
    final RadioButton[] rb = new RadioButton[5];
    RadioGroup rg = new RadioGroup(this); //create the RadioGroup
    rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
    for(int i=0; i<5; i++){
       rb[i]  = new RadioButton(this);          
       rb[i].setText(" " + ContactsActivity.phonetype.get(i)
            + "    " + ContactsActivity.phone.get(i));
       rb[i].setId(i + 100);
       rg.addView(rb[i]);
    }
    ll.addView(rg);//you add the whole RadioGroup to the layout

}

Here is my source code,

First Create String arrays

<string-array name="websites_array">
    <item>Yahoo</item>
    <item>Hotmail</item>
    <item>Gmail</item>
    <item>Facebook</item>
    <item>Other</item>
</string-array>

Then in source code,

final RadioGroup radioGrp = (RadioGroup) findViewById(R.id.radioGroup);

    //get string array from source
    String[] websitesArray = getResources().getStringArray(R.array.websites_array);

    //create radio buttons
    for (int i = 0; i < websitesArray.length; i++) {
        RadioButton radioButton = new RadioButton(this);
        radioButton.setText(websitesArray[i]);
        radioButton.setId(i);
        radioGrp.addView(radioButton);
    }

    //set listener to radio button group
    radioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            int checkedRadioButtonId = radioGrp.getCheckedRadioButtonId();
            RadioButton radioBtn = (RadioButton) findViewById(checkedRadioButtonId);
            Toast.makeText(ConfigurationActivity.this, radioBtn.getText(), Toast.LENGTH_SHORT).show();
        }
    });

That's all... :)


Add array to strings.xml:

<string-array name="some_array">
    <item>some_item_0</item>
    <item>some_item_1</item>
    <item>some_item_2</item>
</string-array>

I don't think you need id but text for each button so put somewhere this method (in my case this is Utils class):

public static void fillRadioGroup(Context context, RadioGroup radioGroup, int stringArrayId){
    for (String s : context.getResources().getStringArray(stringArrayId)){
        RadioButton radioButton = new RadioButton(context);
        radioButton.setText(s);
        radioGroup.addView(radioButton);
    }

    if(radioGroup.getChildCount() > 0)
        radioGroup.check(radioGroup.getChildAt(0).getId());
}

Then you can call it:

Utils.fillRadioGroup(this, radioGroup, R.array.some_array);

To get text you need to call:

if(radioGroup.getChildCount() > 0)
    String text = ((RadioButton) findViewById(radioGroup.getCheckedRadioButtonId())).getText().toString();

try this

RadioButton[] radiobutton = new RadioButton[5];
  final  RadioGroup radiogroup = new RadioGroup(this);

LinearLayout listmainLayout = (LinearLayout) findViewById(R.id.phonelist);
        // listmainLayout.setLayoutParams(new
        // ListView.LayoutParams(width,
        // height / 10));
        listmainLayout.setGravity(Gravity.CENTER_VERTICAL);
        RelativeLayout mainlistLayout = new RelativeLayout(
                getApplicationContext());
        LinearLayout.LayoutParams mainlistLayoutParams = new LinearLayout.LayoutParams(
                (int) (width * 0.85), LayoutParams.WRAP_CONTENT);
        mainlistLayout.setLayoutParams(mainlistLayoutParams);
        // mainlistLayout.setOrientation(LinearLayout.VERTICAL);
        mainlistLayout.setPadding((int) (0.03 * width), 0, 0, 0);
        LinearLayout.LayoutParams radiogroupparams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        radiogroup.setLayoutParams(radiogroupparams);
    for (int i = 0; i < 5; i++) {


        radiobutton[i] = new RadioButton(this);
        radiobutton[i].setText(" " + ContactsActivity.phonetype.get(i)
                + "    " + ContactsActivity.phone.get(i));
        radiobutton[i].setId(i + 100);
        radiogroup.addView(radiobutton[i]);
        mainlistLayout.addView(radiogroup);

}

Tags:

Android