What is the equivalent of "colspan" in an Android TableLayout?

AndroidAndroid LayoutAndroid Tablelayout

Android Problem Overview


I'm using a TableLayout in Android. Right now I have one TableRow with two items in it, and, below that, a TableRow with one item it it. It renders like this:

-----------------------------
|   Cell 1    |  Cell 2     |
-----------------------------
|   Cell 3    |
---------------

What I want to do is make Cell 3 stretch across both upper cells, so it looks like this:

-----------------------------
|   Cell 1    |  Cell 2     |
-----------------------------
|           Cell 3          |
-----------------------------

In HTML I'd use a COLSPAN.... how do I make this work in Android?

Android Solutions


Solution 1 - Android

It seems that there is an attribute doing that : layout_span

UPDATE: This attribute must be applied to the children of the TableRow. NOT to the TableRow itself.

Solution 2 - Android

Just to complete the answer, the layout_span attribute must be added to the child, not to TableRow.

This snippet shows the third row of my tableLayout, which spans for 2 columns.

<TableLayout>
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="@string/create" />
    </TableRow>
</TableLayout>

Solution 3 - Android

And this is how you do it programmatically

//theChild in this case is the child of TableRow
TableRow.LayoutParams params = (TableRow.LayoutParams) theChild.getLayoutParams();
params.span = 2; //amount of columns you will span
theChild.setLayoutParams(params);

Solution 4 - Android

You have to use layout_weight to fill the entire row otherwise it still fills left or right column of table layout.

<TableRow
  android:id="@+id/tableRow1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

 		<Button
 			android:id="@+id/button1"
 			android:layout_width="wrap_content"
 			android:layout_height="wrap_content"
 			android:layout_span="2"
 			android:layout_weight="1"
 			android:text="ClickMe" />
 			     
 	</TableRow>

Solution 5 - Android

Maybe this will help someone. I tried the solution with layout_span but this not working for me. So I solved the problem with this trick. Just use LinearLayout in place of TableRow where you need colspan, that's all.

Solution 6 - Android

use android:layout_span in child element of TableRow element

Solution 7 - Android

I've had some problem with rowspan, in case of TableRow, Textview and so on, generated with code. Even if Onimush answer seems to be good, it don't works with generated UI.

Here is a piece of code which.... don't work:

            TableRow the_ligne_unidade = new TableRow(this);
            the_ligne_unidade.setBackgroundColor(the_grey);

            TextView my_unidade = new TextView(this);
            my_unidade.setText(tsap_unidade_nom);
            my_unidade.setTextSize(20);
            my_unidade.setTypeface(null, Typeface.BOLD);
            my_unidade.setVisibility(View.VISIBLE);

            TableRow.LayoutParams the_param;
            the_param = (TableRow.LayoutParams)my_unidade.getLayoutParams();
            the_param.span = 3;
            my_unidade.setLayoutParams(the_param);
            
            // Put the TextView in the TableRow
            the_ligne_unidade.addView(my_unidade);
            
         

The code seems to be OK but, when you reach the init of "the_params" it returns NULL.

On the other end, this code works like a charm:

            TableRow the_ligne_unidade = new TableRow(this);
            the_ligne_unidade.setBackgroundColor(the_grey);

            TextView my_unidade = new TextView(this);
            my_unidade.setText(tsap_unidade_nom);
            my_unidade.setTextSize(20);
            my_unidade.setTypeface(null, Typeface.BOLD);
            my_unidade.setVisibility(View.VISIBLE);

            // Put the TextView in the TableRow
            the_ligne_unidade.addView(my_unidade);

            // And now, we change the SPAN
            TableRow.LayoutParams the_param;
            the_param = (TableRow.LayoutParams)my_unidade.getLayoutParams();
            the_param.span = 3;
            my_unidade.setLayoutParams(the_param);

The only difference is that I push the Textview inside the TableRow before setting the span. And in this case, it works. Hope this will help someone!

Solution 8 - Android

Actually It is pretty straight forward. This is my solution programmatically

        TableLayout tableLayout = binding.tableLayout;
        TableRow row = new TableRow(this);
        TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
        layoutParams.span = 4;               // define no. of column span will row do
        row.setLayoutParams(layoutParams);

        TextView noDataTextView = new TextView(this);
        noDataTextView.setText("No Data");
        noDataTextView.setGravity(Gravity.CENTER);
        noDataTextView.setLayoutParams(layoutParams);   //This line will span your row

        row.addView(noDataTextView);
        tableLayout.addView(row, 1);

Solution 9 - Android

I think you need to wrap a layout around another one. Have one Layout list vertically, inside have another one (or in this case, two) list horizontally.

I'm still finding it hard to nicely split interface to 50-50 portion in Android.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSpike WilliamsView Question on Stackoverflow
Solution 1 - AndroidSephyView Answer on Stackoverflow
Solution 2 - AndroidMaraguesView Answer on Stackoverflow
Solution 3 - AndroidOnimushaView Answer on Stackoverflow
Solution 4 - AndroidRaghavendraView Answer on Stackoverflow
Solution 5 - AndroidDimanoidView Answer on Stackoverflow
Solution 6 - AndroidMosiurView Answer on Stackoverflow
Solution 7 - AndroidPeterView Answer on Stackoverflow
Solution 8 - AndroidAbdul MuqeetView Answer on Stackoverflow
Solution 9 - AndroidRobGThaiView Answer on Stackoverflow