b85411
b85411

Reputation: 10010

How to get row to extend for full width of the table in Android

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

Upvotes: 0

Views: 442

Answers (1)

Amulya Khare
Amulya Khare

Reputation: 7708

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

...
...

Upvotes: 1

Related Questions