Android - Creating RadioGroup Programmatically - Stack Overflow

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Learn, Share, Build

Each month, over 50 million developers come to Stack Overflow to Google Facebook
learn, share their knowledge, and build their careers. OR

Join the world’s largest developer community.

Creating RadioGroup programmatically

I got a Error while doing the following code

Error:The specified child already has a parent you must call removeView on the child's
parent first

Anyone please help me in resolving this Code:

RadioButton[] radiobutton = new RadioButton[5];


RadioGroup radiogroup = new RadioGroup(this);
for (int i = 0; i < 5; i++) {

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);
radiobutton[i] = new RadioButton(this);
radiobutton[i].setText(" " + ContactsActivity.phonetype.get(i)
+ " " + ContactsActivity.phone.get(i));
radiobutton[i].setId(i + 100);
radiogroup.removeView(radiobutton[i]);
radiogroup.addView(radiobutton[i]);
mainlistLayout.addView(radiogroup);

My logcat shows:

11-12 17:51:11.500: E/AndroidRuntime(3353): FATAL EXCEPTION: main


11-12 17:51:11.500: E/AndroidRuntime(3353): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{com.example.contacts_appetite/com.example.contacts_appetite.ContactDetalisList
java.lang.IllegalStateException: The specified child already has a parent. You must call
removeView() on the child's parent first.
11-12 17:51:11.500: E/AndroidRuntime(3353): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
android.app.ActivityThread.access$600(ActivityThread.java:130)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
android.os.Handler.dispatchMessage(Handler.java:99)
11-12 17:51:11.500: E/AndroidRuntime(3353): at android.os.Looper.loop(Looper.java:137)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
android.app.ActivityThread.main(ActivityThread.java:4745)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
java.lang.reflect.Method.invokeNative(Native Method)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
java.lang.reflect.Method.invoke(Method.java:511)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-12 17:51:11.500: E/AndroidRuntime(3353): at dalvik.system.NativeStart.main(Native
Method)
11-12 17:51:11.500: E/AndroidRuntime(3353): Caused by: java.lang.IllegalStateException:
The specified child already has a parent. You must call removeView() on the child's parent
first.
11-12 17:51:11.500: E/AndroidRuntime(3353): at
android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
android.view.ViewGroup.addView(ViewGroup.java:3249)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
android.view.ViewGroup.addView(ViewGroup.java:3194)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
android.view.ViewGroup.addView(ViewGroup.java:3170)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
com.example.contacts_appetite.ContactDetalisList.getView(ContactDetalisList.java:139)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
com.example.contacts_appetite.ContactDetalisList.onCreate(ContactDetalisList.java:65)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
android.app.Activity.performCreate(Activity.java:5008)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-12 17:51:11.500: E/AndroidRuntime(3353): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-12 17:51:11.500: E/AndroidRuntime(3353): ... 11 more

android

edited Jan 20 '15 at 15:14 asked Nov 12 '13 at 12:33


emaillenin honey
12.7k 10 59 107 173 1 2 12

Please paste ur log – Rohit Nov 12 '13 at 12:34

i think this is wrong RadioButton[] radiobutton = new RadioButton[ContactsActivity.phone .size()]; – Rohit


Nov 12 '13 at 12:34

see this stackoverflow.com/questions/5096329/… – Rohit Nov 12 '13 at 12:36

and layout which u set for radiogroup that will outside the loop, because radiogroup layout set one time now
u add ur buttons in group, so no need to add radiogroup in loop – Rohit Nov 12 '13 at 12:39

Thanks its working – honey Nov 12 '13 at 12:45

4 Answers

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

edited Jul 15 '16 at 12:58 answered Nov 12 '13 at 12:45


V.J. RajaReddy PolamReddy
6,052 1 24 44 16.2k 15 83 147

why do you have rg.addView(rb[i]) before setting text and id? also you are adding view to rg again at the
end – Bhargav Oct 1 '15 at 8:03

yes. that will work... and adding RadioGroup at the end to the parent Layout. – RajaReddy PolamReddy Oct
1 '15 at 8:11

For me, it worked only after removing "rg.addView(radiobutton[i]);" – Ashish John Dec 28 '15 at 11:11

I am also trying this solution but using this way radio group allowing me to select all radio button inside it. It
should be only single selection of radio button within the same radio group. – Min2 Mar 15 '16 at 7:31

don t work where declare ll`? – Webserveis Oct 2 '16 at 10:56

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();

answered Jul 24 at 13:59


Maciek Fryta
1 1

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... :)

answered Jan 11 '16 at 19:29


Günay Gültekin
1,615 2 16 22

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);

answered Nov 12 '13 at 12:43


Rohit
2,295 3 21 49

You might also like