Reputation: 3037
I am generating TableRows dynamically. For each TableRow, there is a TextView in one column and an EditText in the other. How do I generate the rows such that the TextView takes up half the row and the EditText the other half?
This is what I currently have:
The layout xml file
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
android:id="@+id/formTable" />
The Java source file
private TreeMap<String, EditText> textData;
...
protected void onCreate(Bundle savedInstanceState) {
...
textData = populateMap();
...
TableLayout table = (TableLayout) findViewById(R.id.formTable);
configureTableLayout(table,textData);
}
private TreeMap<String, EditText> populateMap() {
TreeMap<String, EditText> map = new TreeMap<String, EditText>();
Set<String> dataTypeSet = app.getWorkingDataTypeList();
for (String dataTypeKey : dataTypeSet) {
EditText edit = new EditText(this);
edit.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
edit.setText("0");
edit.setHorizontallyScrolling(true);
map.put(dataTypeKey, edit);
}
return map;
}
private void configureTableLayout (TableLayout table,
Map<String, ? extends View> input) {
for (Map.Entry<String, ? extends View> entry : input.entrySet()) {
addTableRow(table, entry.getKey(), entry.getValue());
}
}
private void addTableRow(TableLayout table, String dataTypeKey, View v) {
TableRow row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView textCol = new TextView(this); // left column
textCol.setText(app.getTypeNameForKey(dataTypeKey));
textCol.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.FILL_PARENT));
textCol.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
textCol.setHorizontallyScrolling(true);
// right column
TableRow.LayoutParams params = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.column = 1;
v.setLayoutParams(params);
row.addView(textCol);
row.addView(v);
table.addView(row);
}
I have read this post already, but it's not exactly what I'm looking for. My primary motivation for splitting each row evenly is that if one of the Strings resulting from app.getTypeNameForKey(dataTypeKey)
(found in addTableRow()
) is excessively long, it might cause the EditText column to become too small for readability.
Upvotes: 3
Views: 12826
Reputation: 42155
Note that TableLayout
extends LinearLayout
(as does TableRow
, as well). That means any children in the TableLayout
, and any children in the TableRow
, may specify the android:layout_weight
attribute to get the behavior described by Phoenixblade9. Just set android:layout_weight="1"
on both the TextView
and the EditText
, and you'll get 50% splits.
Upvotes: 13
Reputation: 12375
From my previous post :
I don't know that a TableLayout is the best way to do this, it can be cumbersome unless you're displaying large amounts of data and need to use it.
One of the best ways I've found to ensure that form objects have length distributed the way I want them is by using weight rather than explicitly declaring width.
Try the following:
<LinearLayout ... android:orientation="horizontal" ...
android:layout_width="match_parent" android:layout_height="wrap_content"
<TextView ... android:layout_width="0dp" ... android:layout_weight="50" />
<TextView ... android:layout_width="0dp" ... android:layout_weight="50" />
</LinearLayout>
Make sure to declare the layout width as 0, this will let the layout fill to the weight.
This should create two TextViews next to each other on the screen, both filling 50% of the screen. You can play with different percentages. You can also use a LinearLayout as a placeholder with a weight of whatever % you would like to place hold.
Make sure that your "weights" add up to 100 in order to ensure the view will look exactly as you want it to.
You can place these LinearLayouts in a TableLayout if you would like to :)
Hope this helps.
Upvotes: 7