Programmatically adding TableRow to TableLayout not working

android tablelayout add column programmatically
android table layout dynamically add rows and columns
add textview in tablelayout programmatically
custom table layout in android example
add button in table row android
table layout columns android
android populate table layout from database
adaptive table layout android

I'm trying to add table rows programmatically following the code here

    /* Find Tablelayout defined in main.xml */
    TableLayout tl = (TableLayout) findViewById(R.id.SaleOrderLines);
    /* 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. */
    //tr.setBackgroundResource(R.drawable.sf_gradient_03);
    tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

But but nothing is drawn to the screen. (same as one mentioned here)

When i added tr.setBackgroundResource(R.drawable.sf_gradient_03);, the row with the background image is drawn but not the button

Here is my Layout

<!-- Sale Order Lines START -->
<LinearLayout android:id="@+id/SaleOrderLinesArea" android:orientation="vertical"
     android:layout_width="fill_parent" android:layout_height="fill_parent"
     android:padding="10dip" android:background="@drawable/sf_gradient_11" >
    <LinearLayout android:id="@+id/subHeaderArea" android:padding="10dip"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
        >
        <TextView android:id="@+id/screenTitle"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:textColor="@color/title_sub" android:textStyle="bold"
            android:text="Order Lines" />
    </LinearLayout>
    <TableLayout android:id="@+id/SaleOrderLines"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:padding="10dip" android:stretchColumns="*">
        <TableRow
            android:background="@drawable/sf_gradient_13" android:padding="10dip"
            >
            <TextView android:id="@+id/order_ref_label"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:textColor="@color/fg_prime" android:text="bLA bLA" />
            <TextView android:id="@+id/product_label"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:textStyle="bold" android:textColor="@color/fg_title"
                android:text="@string/product" />
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:textStyle="bold" android:textColor="@color/fg_title"
                android:text="@string/product_quantity" />
        </TableRow>
        <TableRow android:background="@drawable/sf_gradient_03"
            android:paddingLeft="10dip" android:paddingRight="10dip"
            >
            <TextView android:id="@+id/order_ref_label"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:textColor="@color/fg_prime" android:text="Fooo" />
            <Spinner android:id="@+id/product_spinner"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:prompt="@string/customer_prompt">
            </Spinner>
            <EditText android:id="@+id/product_uom_qty"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:singleLine="true" android:fadingEdge="horizontal" /> 
        </TableRow>
    </TableLayout>
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:paddingTop="5dip" android:paddingBottom="5dip">
        <Button android:id="@+id/add_button" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Add Lines" />
    </LinearLayout>
</LinearLayout>
<!-- Sale Order Lines END -->

Got it, every LayoutParams should be of android.widget.TableRow.LayoutParams except one that supplied to tl.addView(...)

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

android.widget.TableLayout.addView java code examples, RHE); // delcare a new row TableRow newRow = new TableRow(this); // add views to the row newRow. TextView layout_gravity not working programmatically. I'm trying to add rows to a TableLayout programmatically, but they are not displayed.Displays only the header of the table, which is created on layout. My TableLayout: &lt;TableLayout

Please do note that, may be your code is perfect but the classes you are using are not proper. You may have used import android.view.ViewGroup.LayoutParams , where as the correct class is available from following import statement :

import android.widget.TableRow.LayoutParams

android.widget.TableLayout.setLayoutParams java code examples , setLayoutParams (Showing top 15 results out of 315) How to add a row dynamically in a tableLayout in Android How can i add table row in table layout​? TextView string contents not visible when dynamically creating a tablelayout. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Dynamically add TableRow to TableLayout

In my case, I changed the width from TableRow.LayoutParams.WRAP_CONTENT to TableRow.LayoutParams.MATCH_PARENT, then it works.

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

Android Dynamically Add rows to Table Layout, Android Dynamically Add rows to Table Layout entire string with separator \t and \n but upon rendering on device its not looking good. After adding the columns to the table row its time to add the table row the the main  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

How to add table rows Dynamically in Android Layout?, This example demonstrates how to add table rows Dynamically in Android Layout.Step 1 android:layout_width="match_parent" android:layout_height="​match_parent" > <TableLayout TableRow; import android.widget. Edit: You need to have a separate TableRow XML file that you can access, separate from the actual TableLayout you're trying to dynamically populate. Edit2: I should probably try to make an actual solution for you, so that you see what I'm talking about:

Programmatically adding a TableRow with margins to a TableLayout , I have not been able to give these c# created TableRows margins. To be more clear, what I wish to accomplish in c# is the equivalent of  tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); Here what happens is, you say, add a view into tableLayout and you tell which view it is, and you also say the width and height of the view should wrap it's contents.

[Solved] How to equally divide the table layout height to table rows , Hi, I have the exact same problem. :D It seems, that the weight parameter in a TableRow is just not considered when adding the row to a  android TableLayout TableRow layout_weight. If you face the problem of connecting your Huawei P10 Lite WiFi Hotspot from other devices, or able to connect WiFi Hotspot but the connection is dropped immediately after connected to WiFi Hotspot, then this may be due to WiFi Hotspot Name setup.

Comments
  • Have you done tl.requestLayout();? after adding rows in it?
  • This worked great, thank you. BUT... in my case I had a few hundred rows to add, each with 5 TextView columns. It ended up taking about 7 seconds to create itself. I found instead that a ListView of consistently sized LinearLayouts loaded in just 1 second. That solution was described in a post by EzzyLearning
  • Thanks! It worked. But I don't understand "except one that supplied to tl.addView() " because you used it there as well
  • This seems like a lot of work to add a line to a grid.The new constructor is called 4 times.