Android Stretch columns evenly in a TableLayout - android

I am displaying a table of values in my android application, and would like the columns to be distributed evenly in terms of size , instead of sizing according to content.
Been playing around with stretchColumns but couldn't manage to figure out the right combination, Any Ideas?

I had the same problem - I always only entered one number in android:stretchColumns, but you have to enter all columns that should be stretched. So if you have three columns, you have to write:
android:stretchColumns="0,1,2"
Or write:
android:stretchColumns="*"
Then all columns will have the same size. As a reminder, android:stretchColumns is an attribute for the TableLayout element.

You need to set BOTH android:layout_width="0dip" and android:layout_weight="1" for each view within a table row. I think this works because the weights determine the proportion of the EMPTY SPACE in the row used by the respective views, and since the width of all views is set to 0dip, the whole row is empty space, and hence with equal weights the views are all allocated the same proportion of the WHOLE width.

Use the android:layout_weight for the columns

You control the size of the columns by the size of their contents. If you want them all to be the same size, set that up via android:layout_width in the appropriate cells.

Whoever is still looking for solution, for me:
setting columns both shrinkable and stretchable did the trick.
I had pretty much the same problem as OP.

Related

android GridLayout divider row

I'm trying to implement a tabular layout that has a header and a bunch of rows underneath it. I've chosen the GridLayout (android.support.v7.widget.GridLayout) as there's some requirements for some elements to span multiple columns (but those are of no concern to the question).
My header cells each contain a LinearLayout with a bunch of TextViews, they're dynamically filled in code,for the sake of example, have a look at the image below.
The second row should contain the divider which is a simple view, that should span my header columns (3).
The problem is the width of the divider - if I choose MATCH_PARENT, it will push the GridLayout to fill the whole remaining space to the right. The grid needs to wrap the content and center itself horizontally. It seems to me there's a conflict between the grid's layout (WRAP_CONTENT) and the divider's layout (MATCH_PARENT).
How can I fix the width of the divider without hardcoding it?
http://i61.tinypic.com/2415eg5.png
In red, my LinearLayouts (header), green, the GridLayout itself, the thin blue line at the bottom is the divider.
Thanks,
MO
SOLUTION (as provided below):
I had to set the column weight for the divider to 1, without specifying a width (actually setting it to zero). Because of my specific requirement to handle all of these in code, the solution was to manually instantiate the GridLayout.LayoutParams class and use
ColumnSpec = GridLayout.InvokeSpec(row_index, num_spanned_cols, weight)
Hope this helps others in the future.
If you set the width to 0dp then give it android:layout_weight="1"
(you could give it any weight you want) it should fill all the available space and not push your bounds if I understand what you are asking correctly

Android- How to display items with variable space between then

I have a horizontal layout to display 5 icons.
I would like to know if there is a way to display the first one always X dp from margin left and the last one X dp from the margin right and the others with the same space between then.
With LinearLayout the space is always the same even when I change the device , so sometimes my horizontal row became much more bigger than the space that the icons use and with RelativeLayout i just can align 3 items in this way ( alignParentLeft, centerInParent, alignParentRight)
With RelativeLayout I think you can easily put three of your icons at the proper place. For the last two icons, I don't think there's something you can do in your xml. Actually I have met the same problem before.
An approach is (maybe not the best one) to calculate the margins of the two icons in your java code. It's really easy to get the parent view width (in pixels) and you know how much space(a percentage) there should be before/after your views. Just a multiplication and addView with LayoutParam is sufficient.
A LinearLayout with android:width="match_parent", android:layout_marginRight and android:layout_marginLeft within a RelativeLayout should work.
The Linear Layout will be xdp from the left and xdp from the right, with the icons spaced evenly inside.
If you use the Linear Layout, you can use the weight attribute to calculate the correct spacing between the images.
Set each of their weights to 1 so that they take an equal space in the row :
android:layout_weight="1"
I hope that can assist you.

TableLayout not skipping column 0 when setting android:layout_column = "1"

The xml code on this site: http://www.learn-android.com/2010/01/05/android-layout-tutorial/6/
Gives the table layout shown in the image.
http://www.learn-android.com/wp-content/uploads/2010/01/TableLayout.png
But if you remove the first table row, then the First Name textview appears in column 0 even though its android:column_layout is set to "1"
But on this site: http://developer.android.com/reference/android/widget/TableLayout.html it says "If you skip a column number, it will be considered an empty cell in that row."
IF you want a empty cell in the row, do you have to fill it in with something or set its width?
Is there a better way to get things spaced in the middle of the screen like in the table row?
Thanks
So, the empty cell is there, it just has no width. If there is nothing with a nonzero width, it will look like there is no column 0. To get it to take up some space, there needs to be something there, even if its just an empty View with a width set.
If you want things centered, it might make sense to use a RelativeLayout and use either android:layout_gravity="center" or android:layout_gravity="center_horizontal" to align things in the center of the screen.

Setting column width in Table View

I am having a hard time understanding how to change the width of columns when creating a table view.
I know you can set the width in pixels of a column but that is not what I want to do.
Say, for instance, I have 2 columns and want each column to take up half the screen. How do I do this without explicitly setting the pixels, so that my code can work on multiple screen sizes?
With a TableLayout? You can't do that easily. The closest you can get is to use android:stretchColumns="*" on your TableLayout element. That should allocate any extra whitespace evenly between the two columns.
Just set the TableRow layout_width="0dip"
and then set the two column layout_weight="1"
This will help you evenly divide a row.

How to add the textviews equally in a Row?

i have 5 text views to be added horizontally,and it needs to occupy the displaywidth.When i change the screen orientation,it should change based on it.Is that can be done by Layout or it should be done programmatically
Try using layout_weight parameter. Create a LinearLayout with layout_width="fill_parent" and layout_weightsum="5".
And then add 5 TextViews with layout_width="wrap_content" and layout_weight="1".
You could do that by using android:layout_width="fill_parent" in your text view. This attribute specify that your text views will take up all the parent space in width.
EDIT
Hi..Table layout has a slightly different behaviour.
Quoted from TableLayout:
The width of a column is defined by
the row with the widest cell in that
column. However, a TableLayout can
specify certain columns as shrinkable
or stretchable by calling
setColumnShrinkable() or
setColumnStretchable(). If marked as
shrinkable, the column width can be
shrunk to fit the table into its
parent object. If marked as
stretchable, it can expand in width to
fit any extra space. The total width
of the table is defined by its parent
container. It is important to remember
that a column can be both shrinkable
and stretchable. In such a situation,
the column will change its size to
always use up the available space, but
never more. Finally, you can hide a
column by calling
setColumnCollapsed().
The children of a TableLayout cannot
specify the layout_width attribute.
Width is always FILL_PARENT. However,
the layout_height attribute can be
defined by a child; default value is
WRAP_CONTENT. If the child is a
TableRow, then the height is always
WRAP_CONTENT.
hope this helps
EDIT
When stretchable, a column takes up as much as available space as possible in its row
Suppose you have added the five textview in a column at index col_index in your table. This code will stretch the column.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="col_index"
>
...
</TableLayout>

Categories

Resources