6

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).

gosr
  • 4,593
  • 9
  • 46
  • 82
  • try [requestLayout()](http://developer.android.com/reference/android/view/View.html#requestLayout%28%29) and [this SO-thread](http://stackoverflow.com/questions/5183968/how-to-add-rows-dynamically-into-table-layout) might help. – keyser May 17 '12 at 15:55
  • @Keyser Like `tl.requestLayout();` after the last `addView` statement? Not working I'm afraid. – gosr May 17 '12 at 15:57
  • y that's what I meant...check the SO-thread. Can't help I'm afraid – keyser May 17 '12 at 15:58
  • Try adding `android:orientation:="vertical"` to the `TableLayout`. You will need to add another `TextView` under the first row. – techi.services May 17 '12 at 16:01
  • First see if the table row is being added at all. While the emulator is running, start up the [Hierarchy Viewer](http://developer.android.com/guide/developing/tools/hierarchy-viewer.html) and see if the table rows are visible as children of the `TableLayout` – Ancantus May 17 '12 at 16:09
  • @Ancantus I see the Button and TableRow that I added in the code in `hierarchyviewer`, and they are children of the TableLayout. Also, it says that they are visible (getVisibility() = VISIBLE). – gosr May 17 '12 at 16:27
  • @techiServices That didn't work unfortunately. – gosr May 17 '12 at 16:30

1 Answers1

13

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.

Ancantus
  • 688
  • 6
  • 18
  • 1
    Beat me to it ;) It's only the `LayoutParams` on the `Button` that shouldn't be there since `TableRow` forces them. – techi.services May 17 '12 at 16:48
  • @techiServices Right, I don't think he needs the `TableLayout.LayoutParams` either because it is forced from above in the xml. I may be mistaken, still a bit fuzzy on what layoutparams trump what. – Ancantus May 17 '12 at 17:02