0

I have dynamically created a table that is populated with keys and values of varying length.

When I try to set the width to wrap content, the width is set relative to the individual key sizes rather than to the table as a whole.

This results in the second TextView being too wide once a key with a larger width is added to the table.

I have tried redrawing the table after it has completed, but the original TextView widths are retained.

Is there a simple fix I have overlooked, or do I need to programatically get the longest key width then manually set a maximum width for the second TextView?

layout.xml

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:scrollbars="none">

    <TableLayout
        android:id="@+id/passData"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="15dip">
    </TableLayout>
</ScrollView>

activity.java

private TableRow generateRow(String key, String value, boolean isOdd) {

    TableRow tr = new TableRow(this);
    tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));

    if (isOdd) {
        tr.setBackgroundColor(getResources().getColor(R.color.colorTableRow));
    }

    TextView keyView = new TextView(this);
    TextView valueView = new TextView(this);

    keyView.setTypeface(null, Typeface.BOLD);
    keyView.setPadding(30, 20, 30, 20);
    keyView.setTextSize(15);
    keyView.setText(firstCaps(key));

    valueView.setText(value.replace(" ", "\u00A0"));
    valueView.setSingleLine(false);
    valueView.setMaxLines(20);
    valueView.setPadding(0, 20, 30, 20);
    valueView.setGravity(Gravity.CLIP_HORIZONTAL);

    tr.addView(keyView);
    tr.addView(valueView,new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,     
TableRow.LayoutParams.WRAP_CONTENT));

    return tr;
}

Result

enter image description here

PassKit
  • 12,231
  • 5
  • 57
  • 75

2 Answers2

0

The solution was simply to add a weight to the LayoutParams and set the width to zero to allow it to size correctly.

activity.java

private TableRow generateRow(String key, String value, boolean isOdd) {
    TableRow tr = new TableRow(this);
    tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
TableRow.LayoutParams.WRAP_CONTENT));
    if (isOdd) {
        tr.setBackgroundColor(getResources().getColor(R.color.colorTableRow));
    }
    TextView keyView = new TextView(this);
    TextView valueView = new TextView(this);

    keyView.setTypeface(null, Typeface.BOLD);
    keyView.setPadding(30, 20, 30, 20);
    keyView.setTextSize(15);
    keyView.setText(firstCaps(key));
    valueView.setText(value);
    valueView.setSingleLine(false);
    valueView.setMaxLines(20);
    valueView.setPadding(0, 20, 30, 20);
    tr.addView(keyView);

    // Set width to zero and weight to 1
    tr.addView(valueView,new TableRow.LayoutParams(0, 
        TableRow.LayoutParams.WRAP_CONTENT, 1f));

    return tr;
}

Result

Screenshot

PassKit
  • 12,231
  • 5
  • 57
  • 75
-1

You can use this auto_resize_text view for your problem.

Auto Scale TextView Text to Fit within Bounds

https://github.com/grantland/android-autofittextview

Community
  • 1
  • 1
Tushar Pandey
  • 4,557
  • 4
  • 33
  • 50
  • Not sure this will work as the width for the TextView has already been set. Will try tomorrow and let you know. – PassKit May 31 '16 at 18:14
  • As suspected - the size of the TextView has already been determined relative to the width of the other TextView in the row. This class therefore had no effect. – PassKit Jun 01 '16 at 04:06