Android:StretchColumns: Correct arguments? - android

For the following code:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="2" >
Does android:stretchColumns="2" mean that the 3 columns are stretchable, or that column 3 is stretchable?

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.
Have a look here, keep in mind that if your table fits nicely ( no space left to stretch) in the parent object, you would not see any changes no matter what value you put in.

If u want to stretch all Columns u should use android:stretchColumns="*" it works also to android:shrinkColumns.
And this pro tutorial should explain all yours issues
http://www.onlinevideolecture.com/computer-programming/slidenerd/android-programming-course-android-tutorials-for-beginners/index.php?course_id=2369&lecture_no=59
android:stretchColumns="1" will stretch column 1
android:stretchColumns="0,1" will stretch columns 1 and 0.

Related

tabview column according to screen size

I have a table
<TableLayout
android:id="#+id/branchTable"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/EditTextSubHeading"
android:layout_margin="10dp"
android:collapseColumns="30sp" >
</TableLayout>
I want to fit all column according to screen size.Mean if I have a Tab
10.1" and I have 5 column each column should take 10.1" / 5
try android:layout_span="value"
http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html#attr_android%3alayout_span
you can also try android:layout_column="value"
tutorial http://www.mkyong.com/android/android-tablelayout-example/
Add following code to your tablelayout:
android:stretchColumns="*"
android:shrinkColumns="*"
It means all columns are able to shrink and stretch. Android will handle how to allocate nice space for all columns. You can also specify which columns you want to strech or shrink as well, by setting numbers in the above code.

What is android:weightSum in android, and how does it work?

I want to know: What is android:weightSum and layout weight, and how do they work?
Adding on to superM's and Jeff's answer,
If there are 2 views in the LinearLayout, the first with a layout_weight of 1, the second with a layout_weight of 2 and no weightSum is specified, by default, the weightSum is calculated to be 3 (sum of the weights of the children) and the first view takes 1/3 of the space while the second takes 2/3.
However, if we were to specify the weightSum as 5, the first would take 1/5th of the space while the second would take 2/5th. So a total of 3/5th of the space would be occupied by the layout keeping the rest empty.
Per documentation, android:weightSum defines the maximum weight sum, and is calculated as the sum of the layout_weight of all the children if not specified explicitly.
Let's consider an example with a LinearLayout with horizontal orientation and 3 ImageViews inside it. Now we want these ImageViews always to take equal space. To acheive this, you can set the layout_weight of each ImageView to 1 and the weightSum will be calculated to be equal to 3 as shown in the comment.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- android:weightSum="3" -->
android:orientation="horizontal"
android:layout_gravity="center">
<ImageView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"/>
.....
weightSum is useful for having the layout rendered correctly for any device, which will not happen if you set width and height directly.
Weight sum works exactly as you want (like other answers you don't have to sum all the weights on parent layout). On child view specify the weight you want it to take. Don't forget to specify
android:layout_width="0dp"
Following is an example
<LinearLayout
android:layout_width="500dp"
android:layout_height="20dp" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#android:color/holo_green_light"
android:gravity="center"
android:text="30%"
android:textColor="#android:color/white" >
</TextView>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#android:color/holo_blue_bright"
android:gravity="center"
android:text="20%"
android:textColor="#android:color/white" >
</TextView>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:background="#android:color/holo_orange_dark"
android:gravity="center"
android:text="50%"
android:textColor="#android:color/white" >
</TextView>
</LinearLayout>
This will look like
The documentation says it best and includes an example, (highlighting mine).
android:weightSum
Defines the maximum weight sum. If unspecified, the sum is computed by
adding the layout_weight of all of the children. This can be used for
instance to give a single child 50% of the total available space by
giving it a layout_weight of 0.5 and setting the weightSum to 1.0.
So to correct superM's example, suppose you have a LinearLayout with horizontal orientation that contains two ImageViews and a TextView with. You define the TextView to have a fixed size, and you'd like the two ImageViews to take up the remaining space equally.
To accomplish this, you would apply layout_weight 1 to each ImageView, none on the TextView, and a weightSum of 2.0 on the LinearLayout.
After some experimenting, I think the algorithm for LinearLayout is this:
Assume that weightSum is set to a value. The case of absence is discussed later.
First, divide the weightSum by the number of elements whith match_parent or fill_parent in the dimension of the LinearLayout (e.g. layout_width for orientation="horizontal"). We will call this value the weight multiplier for each element. The default value for weightSum is 1.0, so the default weight multiplier is 1/n, where n is the number of fill_parent elements; wrap_content elements do not contribute to n.
E.g. when weightSum is 60, and there are 3 fill_parent elements, the weight multiplier is 20. The weight multiplier is the default value for e.g. layout_width if the attribute is absent.
Second, the maximum possible expansion of every element is computed. First, the wrap_content elements are computed according to their contents. Their expansion is deducted from the expansion of the parent container. We will call the remainer expansion_remainer. This remainder is distributed among fill_parent elements according to their layout_weight.
Third, the expansion of every fill_parent element is computed as:
Example:
If weightSum is 60, and there are 3 fill_parent elements with the weigths 10, 20 and 30, their expansion on the screen is 2/3, 1/3 and 0/3 of the parent container.
weight | expansion
0 | 3/3
10 | 2/3
20 | 1/3
30 | 0/3
40 | 0/3
The minimum expansion is capped at 0. The maximum expansion is capped at parent size, i.e. weights are capped at 0.
If an element is set to wrap_content, its expansion is calculated first, and the remaining expansion is subject to distribution among the fill_parent elements. If weightSum is set, this leads to layout_weight having no effect on wrap_content elements.
However, wrap_content elements can still be pushed out of the visible area by elements whose weight is lower than (e.g. between 0-1 for weightSum= 1 or between 0-20 for the above example).
If no weightSum is specified, it is computed as the sum of all layout_weight values, including elements with wrap_content set! So having layout_weight set on wrap_content elements, can influence their expansion. E.g. a negative weight will shrink the other fill_parent elements.
Before the fill_parent elements are laid out, will the above formula be applied to wrap_content elements, with maximum possible expansion being their expansion according to the wrapped content. The wrap_content elements will be shrunk, and afterwards the maximum possible expansion for the remaining fill_parent elements is computed and distributed.
This can lead to unintuitive results.
If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0. Must be a floating point value, such as "1.2"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_rel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2.0" >
<RelativeLayout
android:id="#+id/child_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="#0000FF" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/child_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="#00FF00" >
</RelativeLayout>
</LinearLayout>
One thing which seems like no one else mentioned: let's say you have a vertical LinearLayout, so in order for the weights in layout/element/view inside it to work 100% properly - all of them must have layout_height property (which must exist in your xml file) set to 0dp. Seems like any other value would mess things up in some cases.
Layout Weight works like a ratio. For example, if there is a vertical layout and there are two items(such as buttons or textviews), one having layout weight 2 and the other having layout weight 3 respectively. Then the 1st item will occupy 2 out of 5 portion of the screen/layout and the other one 3 out of 5 portion. Here 5 is the weight sum. i.e. Weight sum divides the whole layout into defined portions.
And Layout Weight defines how much portion does the particular item occupies out of the total Weight Sum pre-defined. Weight sum can be manually declared as well. Buttons, textviews, edittexts etc all are organized using weightsum and layout weight when using linear layouts for UI design.
From developer documentation
This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.
Addition to #Shubhayu answer
rest 3/5 can be used for other child layouts which really doesn't need any specific portion of containing layout.
this is potential use of android:weightSum property.
No one has explicitly mentioned that weightSum is a particular XML attribute for LinearLayout.
I believe this would be helpful to anyone who was confused at first as I was, looking for weightSum in the ConstraintLayout documentation.

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>

Android Stretch columns evenly in a TableLayout

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.

Categories

Resources