Dynamically create RadioButton using String Array
I am trying to create a RadioButton from a String Array but it doesn't work, if I only use String then it seems to work fine. Log Cat shows error "ava.lang.IllegalStateException: The specified child already has a parent. You must call removeView () on the parent-parent first."
i know the error occurs because the RadioButton object is not dynamically created, so the error is displayed
I took the code from here for reference, please check this url http://android.okhelp.cz/create-radiobutton-radiogroup-dynamically-android-sample/
Please check the code and correct me so I will go further with my application Here is the code
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
RadioGroup radioGroup = new RadioGroup(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
// adding Radio Group
layout.addView(radioGroup, p);
// creating Radio buttons Object//
RadioButton radioButtonView = new RadioButton(this);
String[] ab ={"1","2"};
for(int i =0; i<ab.length;i++)
{
radioButtonView.setText(ab[i]);
radioGroup.addView(radioButtonView, p);
((ViewGroup)layout.getParent()).removeView(layout);
}
and Log Cat Error
I/Choreographer(4431): Skipped 547 frames! The application may be doing too much work on its main thread.
D/AndroidRuntime(4431): Shutting down VM
W/dalvikvm(4431): threadid=1: thread exiting with uncaught exception (group=0xb2d2fb20)
FATAL EXCEPTION: main
Process: com.example.radiobuttondynamic, PID: 4431
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
at android.view.ViewGroup.addView(ViewGroup.java:3415)
at android.widget.RadioGroup.addView(RadioGroup.java:141)
at android.view.ViewGroup.addView(ViewGroup.java:3391)
at com.example.radiobuttondynamic.Radio_Button.onCreateOptionsMenu(Radio_Button.java:46)
at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:543)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
D/dalvikvm(4431): GC_FOR_ALLOC freed 142K, 7% free 3094K/3308K, paused 107ms, total 113ms
Looking for help, thanks
source to share
IllegalStateException: The specified child already has a parent. You must first call removeView () of the parent parent.
Since you are adding the instance RadioButton
to the LinearLayout
.
Create an RadioButton
in for loop object :
for(int i =0; i<ab.length;i++)
{
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ab[i]);
radioGroup.addView(radioButtonView, p);
((ViewGroup)layout.getParent()).removeView(layout);
}
inside the loop to avoid the error.
source to share
If you have this array: -
String route[] = {"1", "3", "4"};
You can create radio buttons with a loop: -
for (int i = 0; i < route.length; i++)
{
RadioButton radioButton = new RadioButton(this);
radioButton.setText("Route " + String.valueOf(route[i]));
radioButton.setId(i);
rprms = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rgp.addView(radioButton, rprms);
}
And you can get the selected field using: - (I have a SAVE button, so this code is written in an onClick listener) Alternatively, you can also use onCheckedChangeListener.
int selection = rgp.getCheckedRadioButtonId();
for (int i = 0; i < route.length; i++)
{
if (i == selection)
showToast("" + route[i]);
}
source to share
the error is when you are trying to add the same RadioButton multiple times in the same layout, you need to move the Radiobutton creation inside the loop
String[] ab ={"1","2"};
for(int i =0; i<ab.length;i++)
{
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ab[i]);
radioGroup.addView(radioButtonView, p);
((ViewGroup)layout.getParent()).removeView(layout);
}
source to share
This seemed to work for me right out of the box:
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
for (int k = 1; k <= 20; k++) {
//create text button
TextView title = new TextView(this);
title.setText("Question Number:" + k);
title.setTextColor(Color.BLUE);
mLinearLayout.addView(title);
// create radio button
final RadioButton[] rb = new RadioButton[5];
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.VERTICAL);
for (int i = 0; i < 5; i++) {
rb[i] = new RadioButton(this);
rg.addView(rb[i]);
rb[i].setText(countryName[i]);
}
mLinearLayout.addView(rg);
}
Read more: http://www.androidhub4you.com/2013/04/dynamic-radio-button-demo-in-android.html#ixzz5dEVNFDe9
See the full code here http://www.androidhub4you.com/2013/04/dynamic-radio-button-demo-in-android.html
source to share