Tae-Sung Shin
Tae-Sung Shin

Reputation: 20620

TableRow programmatically added does not display

In my xml layout, I have

    <TableLayout
         android:id="@+id/gridview"
         android:layout_height="fill_parent"
         android:layout_width="fill_parent" >
    </TableLayout>

and in activity

    setContentView(R.layout.main);


    /* Find Tablelayout defined in main.xml */
    TableLayout tl = (TableLayout)findViewById(R.id.gridview);
    /* Create a new row to be added. */
    TableRow tr = new TableRow(this);
    tr.setLayoutParams(new 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));

But the button and textview in the tablelayout does not display. Any idea why?

Upvotes: 0

Views: 2765

Answers (2)

Adil Soomro
Adil Soomro

Reputation: 37729

Make sure you use LayoutParams of TableLayout otherwise your LayoutParams will have no effect on TableRow, something like this:

TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));

or simply import only TableLayout.LayoutParams i.e

import android.widget.TableLayout.LayoutParams;

Upvotes: 3

Pratik Bhat
Pratik Bhat

Reputation: 7614

try removing this:

TableRow tr = new TableRow(this);
    tr.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

Upvotes: 0

Related Questions