Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Margin set programmatically on RadioButton not applied

Tags:

android

margin

I'm trying to set a margin to RadioButtons added programmatically to a RadioGroup, but it fails.: the RadioButtons are correctly added, but they have 0 margin...

Anybody can help?

enter image description here

layout

<RadioGroup android:id="@+id/rg_nav" android:orientation="vertical"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
</RadioGroup>

activity

float density = getResources().getDisplayMetrics().density;

rg_nav = (RadioGroup) findViewById(R.id.rg_nav);

LinearLayout.LayoutParams params_rb = new LinearLayout.LayoutParams(
        (int)(8*density),
        (int)(8*density));
int margin = (int)(6*density);
params_rb.setMargins(margin, margin, margin, margin);

for(String url : product.list_url_pic){

    RadioButton radio_btn = new RadioButton(ProductHome.this);  
    radio_btn.setButtonDrawable(R.drawable.rb_nav);
    radio_btn.setId(rb_id++);
    rg_nav.addView(radio_btn, params_rb);
}  
like image 468
jul Avatar asked Oct 26 '11 16:10

jul


2 Answers

I had to replace

LinearLayout.LayoutParams params_rb = new LinearLayout.LayoutParams(
    (int)(8*density),
    (int)(8*density));

by

RadioGroup.LayoutParams params_rb = new RadioGroup.LayoutParams(
                (int)(8*density),
                (int)(8*density));
like image 94
jul Avatar answered Oct 11 '22 12:10

jul


Try this:

TableRow.LayoutParams rg_params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 3f);
RadioGroup radio_group=new RadioGroup(this);
radio_group.setOrientation(RadioGroup.HORIZONTAL);
radio_group.setLayoutParams(rg_params);

RadioButton dry=new RadioButton(this);
RadioGroup.LayoutParams params_soiled = new RadioGroup.LayoutParams(getBaseContext(), null);
params_soiled.setMargins(10, 0, 10, 0);
dry.setLayoutParams(button_params);
like image 32
Issac Balaji Avatar answered Oct 11 '22 13:10

Issac Balaji