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!!
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!