0

I followed the example at https://stackoverflow.com/a/7123217/2884981 but unfortunately, the row is not extending for the full width of the table (see how the black "border"/background is so large on the right while it's only 1px wide everywhere else).

I want the second column to extend, so it's taking up all the available space to the right.

Border is too big

Here is the code I am using. Please, if anyone knows how I can get that right "border" to 1px instead of it looking so awful, let me know.

        for (int i = 0; i < tasks.size(); i++)
    {
        TaskDetail taskDetail = tasks.get(i);

        TableRow row = new TableRow(this);

        // Set the background/border colour to black
        row.setBackgroundColor(Color.BLACK);
        row.setPadding(1, 1, 1, 1);

        TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        layoutParams.setMargins(0,0,1,0);

        LinearLayout detailCell = new LinearLayout(this);
        detailCell.setBackgroundColor(Color.parseColor("#FFE6F0"));
        detailCell.setLayoutParams(layoutParams);

        LinearLayout valueCell = new LinearLayout(this);
        valueCell.setBackgroundColor(Color.WHITE);
        valueCell.setLayoutParams(layoutParams);    

        TextView detailName = new TextView(this);
        detailName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        detailName.setText(taskDetail.getName());
        detailName.setPadding(5, 10, 5, 5);
        detailName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        TextView valueName = new TextView(this);
        valueName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        valueName.setText(taskDetail.getValue());
        valueName.setPadding(5, 10, 5, 5);
        valueName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        detailCell.addView(detailName);
        valueCell.addView(valueName);

        // Add to the screen layout
        row.addView(detailCell);
        row.addView(valueCell);

        table.addView(row);
    }
Community
  • 1
  • 1
b85411
  • 9,420
  • 15
  • 65
  • 119
  • Please follow this link: http://stackoverflow.com/questions/9719380/how-to-set-table-width-and-number-of-columns-in-tablelayout-in-android – Pihu Nov 06 '13 at 04:17

1 Answers1

1

Set weight to be 1.0f for each column in the row as follows:

TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);

Simply make changes as follows:

...
...

row.setPadding(1, 1, 1, 1);

TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
layoutParams.setMargins(0,0,1,0);

LinearLayout detailCell = new LinearLayout(this);

...
...
Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
  • In case anyone else is curious, another way I found to do this is to add detailName.setWidth(0); and valueName.setWidth(0); as well as table.setStretchAllColumns(true); By doing this, you can programatically set up the table layout so that the columns have equal widths. – b85411 Nov 06 '13 at 07:26