Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Views added to TableLayout programmatically not showing

I'm trying to add views to a TableLayout, but they simply won't show up (I'm testing on an emulator). Actually I wrote more code than this, i.e. adding textviews to tablerows, but I've cut it down to this, and even this won't work. Any idea why?

Here's the code of test.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEST" >
    </TextView>
</TableRow>

</TableLayout>

And the java code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.test);

    /* Find Tablelayout defined in test.xml */
    TableLayout tl = (TableLayout) findViewById(R.id.tableLayout);
    /* Create a new row to be added. */
    TableRow tr = new TableRow(this);
    tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    /* Create a Button to be the row-content. */
    Button b = new Button(this);
    b.setText("Dynamic Button");
    b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    /* Add Button to row. */
    tr.addView(b);
    /* Add row to TableLayout. */
    tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
}

When I test on the emulator I only see the textview with the hardcoded string "TEST" (defined in the xml file).

like image 874
gosr Avatar asked May 17 '12 15:05

gosr


1 Answers

Try not setting the buttonLayout and not adding a TableLayout.LayoutParams when adding the row to the table.

//b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr.addView(b);
//tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
t1.addView(tr)

I think the TableLayout.LayoutParams is causing some issues.

like image 61
Ancantus Avatar answered Oct 31 '22 01:10

Ancantus