views:

601

answers:

3

Hi,

I'm trying to create a TableLayout programatically. It just won't work. The same layout in an xml file works though. This is what I have:

public class MyTable extends TableLayout
{
    public MyTable(Context context) {
        super(context);

        setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        TableRow row = new TableRow(context);
        row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        Button b = new Button(getContext());
        b.setText("hello");
        b.setLayoutParams(new LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        row.addView(b); 
        addView(row)
    }
}

...

// In main activity:
MyTable table = new MyTable(this);
mainLayout.addView(table);

When I run this, I don't get a crash, but nothing appears. If I get rid of the TableRow instance, at least the button does appear as a direct child of the TableLayout. What am I doing wrong?

Thanks

+2  A: 

It turns out I needed to specify TableRowLayout, TableLayout etc for the layout params, otherwise the table just won't show!

mark
A: 

hey .. please I have the same problem, and I dont know how to solve it so far, did you have the solution ??

Muath
+1  A: 

I followed this simple tutorial : http://huuah.com/using-tablelayout-on-android/

and everything ran well

mRbAdAw