Adding TableRow programmatically in Android

I'm working in an Android application and I want to add a TableRow programmatically in my TableLayout.

I have this TableLayout:

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

<TableRow>
    <TextView
        android:text="4686"
        android:layout_width="wrap_content"
        android:layout_column="0"
        android:layout_weight="1"
        android:textSize="7px"
        android:textColor="@color/black"  />
    <TextView
        android:text="sdhiuf osdfh isdhf ihdf"
        android:layout_width="wrap_content"
        android:layout_column="1"
        android:layout_weight="1"
        android:textSize="7px"
        android:textColor="@color/black"  />
    <TextView
        android:text="2"
        android:layout_width="wrap_content"
        android:layout_column="2"
        android:layout_weight="1"
        android:textSize="7px"
        android:textColor="@color/black" />
    <TextView
        android:text="UN"
        android:layout_width="wrap_content"
        android:layout_column="3"
        android:layout_weight="1"
        android:textSize="7px"
        android:textColor="@color/black" />
</TableRow>

and I want to add this exactly TableRow programmatically.

I'm trying something like this:

TableLayout detailsTable = (TableLayout) l.findViewById(R.id.details_table);

for(Nfce_Product nfceProduct : nfceProducts){
    TableRow tableRow = new TableRow(getActivity());

    TextView tvProductCode = new TextView(getActivity());
    tvProductCode.setLayoutParams(new TableRow.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
    tvProductCode.setText(nfceProduct.getProduct_code());
    tvProductCode.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
    tvProductCode.setTextColor(getResources().getColor(R.color.black));

    TextView tvProductDescription = new TextView(getActivity());
    tvProductDescription.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
    tvProductDescription.setText(nfceProduct.getProduct_description());
    tvProductDescription.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
    tvProductDescription.setTextColor(getResources().getColor(R.color.black));

    TextView tvProductAmount = new TextView(getActivity());
    tvProductAmount.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
    tvProductAmount.setText(String.valueOf(nfceProduct.getAmount()));
    tvProductAmount.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
    tvProductAmount.setTextColor(getResources().getColor(R.color.black));

    TextView tvProductMetric = new TextView(getActivity());
    tvProductMetric.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
    tvProductMetric.setText(nfceProduct.getProduct_metric());
    tvProductMetric.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
    tvProductMetric.setTextColor(getResources().getColor(R.color.black));

    tableRow.addView(tvProductCode);
    tableRow.addView(tvProductDescription);
    tableRow.addView(tvProductAmount);
    tableRow.addView(tvProductMetric);
    detailsTable.addView(tableRow);
}

Upvotes: 2

Views: 8400

Answers (1)

Steven_BDawg
Steven_BDawg

Reputation: 806

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.

Upvotes: 8

Related Questions