Programmatically Adding Tablerow to Tablelayout Not Working

Programmatically adding TableRow to TableLayout not working

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

Adding TableRow programmatically in Android

Here is my own answer to my own dynamically created TableRow question. I think my answer is detailed enough, you should have no problems taking it as your own! My issue involved not only dynamically creating TableRows, but also being able to touch each row and have something happen. I've gone further than that nowadays to make each CELL separately clickable.

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:

First, create (or keep) your XML file containing your TableLayout. Second, Create a separate TableRow XML file.

(This is your potential tablerow.xml file)

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:focusable="true" >
<TextView
android:id="@+id/tableCell1"
android:layout_width="wrap_content"
android:layout_column="0"
android:layout_weight="1"
android:textSize="7px"
android:textColor="@color/black" />

<TextView
android:id="@+id/tableCell2"
android:layout_width="wrap_content"
android:layout_column="1"
android:layout_weight="1"
android:textSize="7px"
android:textColor="@color/black" />

<TextView
android:id="@+id/tableCell3"
android:layout_width="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:textSize="7px"
android:textColor="@color/black" />

<TextView
android:id="@+id/tableCell4"
android:layout_width="wrap_content"
android:layout_column="3"
android:layout_weight="1"
android:textSize="7px"
android:textColor="@color/black" />
</TableRow>

Now... back to your actual code!

for (Nfce_Product nfceProduct : nfceProducts) {
final TableLayout detailsTable = (TableLayout) findViewById(R.id.details_table);
final TableRow tableRow = (TableRow) getLayoutInflater().inflate(R.layout.tablerow, null);
TextView tv;

//Filling in cells
tv = (TextView) tableRow.findViewById(R.id.tableCell1);
tv.setText(nfceProduct.getProduct_code());

tv = (TextView) tableRow.findViewById(R.id.tableCell2);
tv.setText(nfceProduct.getProduct_description());

tv = (TextView) tableRow.findViewById(R.id.tableCell3);
tv.setText(nfceProduct.getAmount());

tv = (TextView) tableRow.findViewById(R.id.tableCell4);
tv.setText(nfceProduct.getProduct_metric());

//Add row to the table
detailsTable.addView(tableRow);
} //End for

If you still need/want to change the text size and color, you can do that after the setText() line and before you do findViewById again. If you want to have column headers that basically say Code, Description, Amount, and Metric, make a TableRow inside the TableLayout like you have currently. The TableRows created programmatically will fall in line after that "header" row.

Views added to TableLayout programmatically not showing

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.

Dynamically add TableRow to above of TableLayout

According to Android Team's answer I realized I must change this line:

table.addView(row);

to this:

table.addView(row, 0);

How to add table rows to table layout programmatically?

Move following line after super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Dynamically add TableRow to TableLayout

As it turns out, Eclipse is not always right. Ctrl+Shift+M made the wrong import. It had import android.view.ViewGroup.LayoutParams when it should have import android.widget.TableRow.LayoutParams

Everything works fine now.



Related Topics



Leave a reply



Submit