TableRow span not working for dynamically added rows
Asked Answered
P

2

5

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: Span layout problem

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.

TestProject.java

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

main.xml

<?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>

table_row_columns.xml

<?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>

table_span_row.xml

<?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

Peephole answered 23/5, 2011 at 18:35 Comment(1)
i found the answer to similar issue in this https://mcmap.net/q/102750/-set-the-layout-weight-of-a-textview-programmaticallyScuta
P
2

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

Peephole answered 27/5, 2011 at 8:1 Comment(0)
B
1

May be it will help to somebody. Even this question was more than 2 years I see the same bug right now. That is, rowSpanLayout.span = 2; doesn't work. But in xml layout_span it works. I found the workable solution: 1. create the layour xml file injection with layout properties:

 <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">
     <TableRow 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">
          <TextView
                android:id="@+id/check1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_span="3"
                android:text="TextView" />
     </TableRow>
     </TableLayout>
  1. Get Layout properties from this layout element:
TableRow.LayoutParams blp;
blp = (TableRow.LayoutParams)check1.getLayoutParams();
blp.width = 100;
blp.height = 50;
TextView tw = new TextView(this);
tw.setTextSize(tsEL);
tw.setLayoutParams(blp);
tw.setText(R.string.empty);
tr.addView(tw);

It works.

Bolen answered 8/9, 2013 at 9:37 Comment(2)
Just a question: in the XML file, you have the object check1. You get the params from it, then use them for tw. How can you do if you don't have check1? Because f I try to create tw then do blp = (TableRow.LayoutParams)check1.getLayoutParams();, i get blp null.County
i've found. have a look here to see how to avoid need of check1. #2711293County

© 2022 - 2024 — McMap. All rights reserved.