tags:

views:

23

answers:

1

I'm writing a task manager app that downloads a list of tasks and subtasks from a server and creates a new checkbox for each item and adds it to a linear layout (called ll below). The problem I'm having is that I cannot set the "layout margin left" using java like I can with XML (this is for the subtasks to indent them a bit on the screen). I can set most other xml properties, but cb.setMargins() doesn't work (says undefined for type checkbox). Setting the padding of course does not achieve the desired result.

for(int i=0;i<tasks.size();i++) {
CheckBox cb = new CheckBox(this);
cb.setText(tasks.get(i).subtask_desc);
cb.setButtonDrawable(R.drawable.checkbox_xml);
ll.addView(cb);
    }

Any ideas or how I would work through this?

A: 

I think you should add the checkbox to the LinearLayout using the correct LayoutParams:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = 123;
li.addView(cb, params);

Hope that helps!

lencinhaus
I've seen that code 100 times just never realized how to use it properly. I always thought that created a new linear layout. This worked perfectly and was very helpful, thank you.
Satchmo