Pages
Table layout
8:44 AM
Posted by Mina Samy
In android you define the number of rows by your own and android determines the number of cells in each row according to the number of widgets in each row
You can define a table and add rows and views to it like this:<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" />
TableLayout tl=new TableLayout(this);
TableRow tr=new TableRow(this);
Button btn=new Button(this);
btn.setText("Hello");
tr.addView(btn);
tl.addView(tr);
setContentView(tl);
This example represents a row with three cells.A table cell can span multiple columns like this:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" />
Also you can choose which column to put your widget by using android:layout_column property, you define the Zero-based index of the column where you want your widget to be.
Note: if you specify an index greater than the actual cells count, the widget won’t appear. For example in the previous example if android:layout_column had a value greater than 2 the widget wouldn’t appear.<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Help" android:layout_column="1" />
Table layout also allows you to put widgets directly under the <TableRow> tag to act as a separator between rows.
These widgets will have their width set to fill_parent.
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Help" android:layout_column="5" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Separator" />
In table layout each column occupies space equal to the size of the largest widget in it. But you can set the width of any column to take the largest available space, just like setting the width to 100 % in HTML. This is done by setting the property android:stretchColumns to the index of the column, also you can set multiple columns by separating them with a comma.
Look at the layout below:
Now we gonna to add android:stretchColumns="0" to the table layout and see what it’s gonna look like:
Column 0 occupied the largest available space.
You can set this property from code like this
TableLayout tl=new TableLayout(this); tl.setColumnStretchable(0, true);the setColumnStretchable(ColumnIndex, IsStretchable)method parameters are the column index and a Boolean value to indicate it is going to be stretched.
Now if we have a column that have a large content. Android columns by default do not wrap their content. Look at this layout:
See column zero occupies large space that column 1 is not visible.
We can use android:shrinkColumns property to wrap content of a certain column or for multiple columns by assigning column numbers separeted by commas.
When we use the property with column zero it will be like this:
Its like using style=”white-space:wrap;” style in HTML
You can set the property from code like this:
TableLayout tl=new TableLayout(this); tl.setColumnShrinkable(0, true);The setColumnShrinkable(columnIndex, isShrinkable) method parameters are the column index and a Boolean to indicate it si going to be shrinked.
Finally if you want to make some columns invisible you can use the property android:collapseColumns the same way we used the last two properties
See that column zero is invisible.
You can use this property from code like this:
TableLayout tl=new TableLayout(this); tl.setColumnCollapsed(0, true);The property setColumnCollapsed(columnIndex, isCollapsed) parameters are the column index and a Boolean to indicate that it’s going to be collapsed
This is just like using the style=”display:none;” in HTML
Other functions can be called from code:
TableLayout.setShrinkAllColumns(Boolean shrinkAllColumns) : shrinks all colums
TableLayout.setStretchAllColumns(Boolean stretchAllColumns): stretches all columns
| Reactions: |
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
Layout Resources,
Table Layout
. Follow any responses to this post through RSS. You can leave a response, or trackback from your own site.
Subscribe to:
Post Comments (Atom)









September 3, 2010 5:51 PM
Thanks for sharing this! It's about time someone just explains this simply. It helped me a lot.
September 3, 2010 7:55 PM
You're welcome. I'm glad it helped
September 14, 2010 1:51 PM
Hi Mina, thanks a lot for explaining the table layout extensively. I have one more question and would be grateful if you would have the answer for me.
Question: Is it possible to dynamically set a span on a column of a row using code? I would like to have a title which spans the entire table width and the next row containing for example 5 columns. Is this possible using code instead of layout xml files. Is it also possible for row span?
Thanks a lot for your time and good work!
September 14, 2010 4:00 PM
Hi All,
I Found the solution to the cell span issue and wanted to share it with you. The example shown below will create a title row containing a textview with a span of 5 cells:
// Create the layout params which will be used for the textview
TableRow.LayoutParams trParams = new TableRow.LayoutParams();
trParams.span = 5;
TableRow titleRow = new TableRow(this);
// Create TextView and set layout params (this is the bit which does the trick)
TextView title = new TextView(this);
title.setLayoutParams(trlp);
titleRow.addView(title);
October 10, 2010 9:40 AM
Thanks Ali for sharing this info with us
January 28, 2011 7:41 PM
I would like to be able to click on the first row to bring up a dialog, How can I do this ?
February 15, 2011 8:32 AM
This is really helpful.
Google suck at explaining stuff beyond "helloworld", they expect that we're all Phd's in Computer Science (Elitest scum!)
Well done!
June 2, 2011 2:45 PM
Thanks
October 10, 2011 9:18 AM
Very systematically explained.Your post helped me a lot.
November 11, 2011 7:40 PM
" " gives me an error. Ideas?
November 17, 2011 8:45 PM
Thanks you very much, it is really helpful.
January 18, 2012 1:52 PM
Thanks! May God bless you.