Bizzare android table row issue

Arnav Sengupta

I am programmatically trying to add views to my table layout in Android. But I am not able to add more than 1 view to my row. It only renders the first view. The second column is empty. My code is as below.

    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    TableRow tr1 = new TableRow(this);
    TableRow tr2 = new TableRow(this);
    TextView productNumber = new TextView(this);
    productNumber.setText("PRODUCT NUMBER: " + b[0].getProduct_id());
    productNumber.setTextSize(18);
    productNumber.setTextColor(Color.RED);
    productNumber.setPadding(20, 5, 20, 5);
    tr1.addView(productNumber);
    TextView productDesc = new TextView(this);
    productDesc.setText("PRODUCT DESCRIPTION: " + b[0].getItem_description());
    productDesc.setTextSize(18);
    productDesc.setTextColor(Color.RED);
    productDesc.setPadding(20, 5, 20, 5);
    tr2.addView(productDesc);
    productNumber.setLayoutParams(lp);
    productDesc.setLayoutParams(lp);
    tl.setStretchAllColumns(true);
    tl.addView(tr1, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
    tl.addView(tr2, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));



    for (ProductBean p: b) {
        TableRow lineSeparator = new TableRow(this.getApplicationContext());
        View line = new View(this.getApplicationContext());
        line.setMinimumHeight(1);
        line.setBackgroundColor(Color.WHITE);
        line.setPadding(0, 5, 0, 5);
        lineSeparator.addView(line);
        tl.addView(lineSeparator);
        Set<String> headers = p.getAttr().keySet();

    //Till here it works fine. The following code seems to not add more than 1 view

        for (String s: headers) {
            Log.i("S", "S: " + p.getAttr().get(s));
            TableRow tableRow = new TableRow(getApplicationContext());
            TextView textView = new TextView(getApplicationContext());
            textView.setText(s);
            textView.setPadding(20, 5, 20, 5);
            textView.setTextSize(15);
            tableRow.addView(textView);
            textView = new TextView(getApplicationContext());
            textView.setText(p.getAttr().get(s));
            textView.setPadding(20, 5, 20, 5);
            textView.setTextSize(15);
            tableRow.addView(textView); //This view is not rendered.
            tl.addView(tableRow);
        }
    }

However, I tried the following in a separate activity and layout and it worked successfully. Hello is one column and World another.

    for (int i=0; i<5; i++) {
        TableRow tableRow = new TableRow(getApplicationContext());
        TextView textView = new TextView(getApplicationContext());
        textView.setText("Hello");
        tableRow.addView(textView);
        textView = new TextView(getApplicationContext());
        textView.setText("World"); 
        tableRow.addView(textView);
        tableLayout.addView(tableRow);
    }

Why is my code not rendering the second view then? I am able to log the contents of both the textviews but it just doesn't render the second view in the row. Please help. I am utterly lost, been beating my head over it for 12 hours now!!

Arnav Sengupta

I figured the problem and the solution as well. Actually my xml had just one scroll view and one table layout within that. The first two textviews that I add and then the table rows that I add, would span the width of the entire screen and thus the first column of the table would occupy the whole width of the screen.

   tl.addView(tr1, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
   tl.addView(tr2, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

Now I have created a separate LinearLayout outside the scroll view and adding the content of these two rows above to the LinearLayout. Works fine!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android Eclipse - Table Row Button - Formatting Issue

From Dev

Table Row attr() Issue

From Dev

Issue with table row acting as a link

From Dev

Mysql, Issue with inserting a row into a table

From Dev

Table with dynamic row height issue

From Dev

Mysql, Issue with inserting a row into a table

From Dev

Issue with table row acting as a link

From Dev

Table with dynamic row height issue

From Dev

Programming issue with no row at position in that table

From Dev

Bizzare "attempt to call a table value" in Lua

From Dev

Issue on Drawing Highcharts From Selected Row Table

From Dev

Issue changing table row selection color

From Dev

Smart-Table row selection issue

From Dev

jQuery - issue inserting new row in html table

From Dev

Android table row align to the bottom

From Dev

Border for table row items in android

From Dev

align icons on table row android

From Dev

Android SQLite issue - table __ has no column named ___

From Dev

having issue with Table creation in SQLite in Android

From Dev

Having issue deleting a html table row using jquery dataTable

From Dev

HTML Table Row nth-child Dynamic Content Issue

From Dev

Issue on Removing A Table Row Which All <td> has a Specific Class

From Dev

Android - add header to table row (ArrayAdapter)

From Dev

Android Table row, cannot center text

From Dev

getting single row from table in sqlite android

From Dev

Android Sqlite: Check if row exists in table

From Dev

Making table row scrollable in Java in Android Stud

From Dev

How to return a Table row in Android SQLite

From Dev

Android Display Selected Table Row in Dialog