I\'m trying to create a RadioGroup
within an Android layout where the child RadioButton
s are stretched to evenly fill the entire width of the
I ran into this issue too, I used the RadioGroup.LayoutParams
with weight defined. However I also found once I'd created programatically the buttons weren't responding to touch so set clickable
and enabled
to true
and that fixed things.
private RadioButton createMyTypeRadioButton(MyType type){
//create using this constructor to use some of the style definitions
RadioButton radio = new RadioButton(this, null, R.style.MyRadioStyle);
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
radio.setLayoutParams(layoutParams);
radio.setGravity(Gravity.CENTER);
//tag used by the setOnCheckedChangeListener to link the radio button with mytype object
radio.setTag(type.getId());
//enforce enabled and clickable status otherwise they ignore clicks
radio.setClickable(true);
radio.setEnabled(true);
radio.setText(type.getTitle());
return radio;
}
private void updateMyTypesUi() {
//populate RadioGroup with permitted my types
for (int i = 0; i < myTypes.size(); i++) {
MyType type = myTypes.get(i);
RadioButton radioButton = createSwapTypeRadioButton(type);
myRadioGrp.addView(radioButton);
}
myRadioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checkedType = (RadioButton) group.findViewById(checkedId);
String idOfMyTypeChecked = (String) checkedType.getTag();
//do something with idOfMyTypeChecked
}
});
}
FYI, to do it without any xml at all
RadioGroup rgrp = new RadioGroup(context);
rgrp.setLayoutParams(new RadioGroup.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
rgrp.setOrientation(LinearLayout.HORIZONTAL);
mAccent = new RadioButton(context);
mAccent.setText("Accent");
mAccent.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
rgrp.addView(mAccent);
mGhost = new RadioButton(context);
mGhost.setText( "Ghost");
mGhost.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
rgrp.addView(mGhost);
mFlam = new RadioButton(context);
mFlam.setText( "Flam");
mFlam.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
rgrp.addView(mFlam);
layout.addView(rgrp);
When you set a layout weight, you should use fill_parent as layout width. Then you shouldn't use LinearLayout.LayoutParams but RadioGroup.LayoutParams, as you're adding radio buttons to a RadioGroup, not to a simple LinearLayout.
Finally, as you use the inflater to "build" the radio button, the XML file of the radio button already has the layout params picked from the XML file, so I think you should just call the addView method that takes only the view to add as parameter (that is addView(View v)
) and change the layout_width to fill_parent.
Note that, if you'll need to reference the variable "button" in the code, i.e. add a click listener, you'll add the listener only to the last created button. You'll have to create a RadioButton object for each RadioButton you will add to the RadioGroup (button, button1, button2, etc).