Reputation: 4991
In my app I need to create a RadioGroup
with 2 RadioButtons
by code. I need to align the RadioButtons inside the RadioGroup
, one to the left, and the other to the right part of RadioGruop
. I know that if I add the radio-buttons to a LinearLayout these will solve this problem but in this case the property of RadioGroup, that only one RadioButton is checked at a time is not anymore available.
Here is my code :
TableRow.LayoutParams lp_radio1 = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams lp_radio2 = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams lp_radiogr = new TableRow.LayoutParams(
width, height);
lp_radio1.setMargins(left, left, top2, 0);
lp_radio1.gravity=Gravity.LEFT;
lp_radio1.weight=1;
lp_radio2.setMargins(left, left, top2, 0);
lp_radio2.gravity=Gravity.RIGHT;
lp_radio2.weight=1;
product_radiogroup = new RadioGroup(viewToLoad.getContext());
product_radiogroup.setLayoutParams(lp_radiogr); product_radiogroup.setOrientation(RadioGroup.HORIZONTAL); product_radiogroup.setBackgroundResource(R.drawable.radio_group_background);
product_radiobuttonYES = new RadioButton(viewToLoad.getContext());
product_radiobuttonYES.setLayoutParams(lp_radio1);
product_radiobuttonYES.setTextColor(R.color.medium_gray);
product_radiobuttonNO = new RadioButton(viewToLoad.getContext());
product_radiobuttonNO.setLayoutParams(lp_radio2);
product_radiobuttonNO.setTextColor(R.color.medium_gray);
product_radiogroup.addView(product_radiobuttonYES);
product_radiogroup.addView(product_radiobuttonNO);
and here is my result :
Has anyone any idea how to solve this ? Thanks in advance.
Upvotes: 1
Views: 551
Reputation: 1728
i don't think this solution is a nice one, but since you know the exact width
and height
of your radiogroup
, just set the width
of your radiobutton
to width/2
instead of wrap_content
and other attributes, such as weight
and gravity
on lp_radio1
and lp_radio2
can be removed.
Upvotes: 1