I need to dynamically add RadioButtons to the app, and when I add either buttons or group to the layout it works perfectly. But when I try to add RadioButtons to RadioGroup app crashes with cast exception: android.widget.RadioButton cannot be cast to android.widget.LinearLayout
I've used this post for reference: Creating RadioButtons programmatically. It must be something really basic, but I've been playing with it for a while now, so a bit overwhelmed trying to solve it.
Here is my code:
protected void addRadioButtons(LinearLayout layout, String switchOptions){
String[] optionsArray = switchOptions.split(",");
int numberOfOptions = optionsArray.length;
RadioButton[] radioButton = new RadioButton[numberOfOptions];
RadioGroup radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.HORIZONTAL);
LayoutParams radioGroupParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
radioGroup.setLayoutParams(radioGroupParams);
//layout.addView(radioGroup);
for (int i = 0; i<numberOfOptions;i++){
radioButton[i] = new RadioButton(this);
radioButton[i].setText(i + "");
radioGroup.addView(radioButton[i]);
}
layout.addView(radioGroup);
}