I have the following problem spanning dynamically added rows to a TableLayout inside a scroll view. The rows follow this pattern:
Row 1: Cell spanned over the whole table
Row 2: Two cells
Row 3: Cell spanned over the whole table
...
Row N: Two cells
The problems is that the row with the one cell spanning over the row actually does not span at all. It reaches at some point in the middle of the screen and just wraps at height.
Here is a screenshot of the problem under Android 2.3.3:
Note that the samples below are oversimplified (but still spanning does not work). They are ready to try - just create the files. I debugged and it seems that the layout params are somehow disappearing along the way. Additionally, if the TableLayout is hard-coded in the main.xml file, then there are no problems. Unfortunately, I need to have views dynamically generated.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class TestProject extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout table = new TableLayout(this);
ScrollView contentHolder = (ScrollView) findViewById(R.id.scrollView1);
contentHolder.addView(table, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
TableRow row1 = (TableRow) View.inflate(this, R.layout.table_span_row, null);
TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
rowSpanLayout.span = 2;
table.addView(row1, rowSpanLayout);
TableRow row2 = (TableRow) View.inflate(this, R.layout.table_row_columns, null);
table.addView(row2);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<ScrollView android:id="@+id/scrollView1" android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ScrollView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content" android:text="This is column one" android:layout_weight="1" android:layout_width="fill_parent"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Column 2"></TextView>
</TableRow>
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_span="2" android:background="#FF333333">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="This should span on the whole row"></TextView>
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="... but it does not do so"></TextView>
</LinearLayout>
</TableRow>
I have tried requestLayout and invalidate but to no avail.
P.S. I also welcome advises about how to dynamically replace views within the ScrollView
OK, I found the solution. It was simply to drop the TableRow tag for the row that I wanted to span across the table.
table_span_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="#FF333333">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:text="This should span on the whole row" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="... and now it does" />
</LinearLayout>
I do not know what will happen if I had 3 columns instead and wanted to span one of the cells on two of them :)