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