Android Layout weight - android

I wanted to represent a table. I have a parent container in a LinearLayout with vertical orientation and each row is further represented by a LinearLayout with horizontal orientation & weight of 100. I have used weight to ensure that the left side column and right side columns get 50% of screen width. But the columns on the right side are not properly aligned for some rows. What can I do to properly align them as in a table?
This is how my code appears for each LinearLayout row represented below
<LinearLayout
android:orientation="horizontal"
android:weightSum="100"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BOM ID"
android:id="#+id/tvBOMID" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/etBOMID"
android:hint="BOM ID"
android:layout_weight="50" />
</LinearLayout>

Simply give 1-1 equal weight to TextView,EditText and set width as 0dp :
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="BOM ID"
android:id="#+id/tvBOMID" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/etBOMID"
android:hint="BOM ID"
android:layout_weight="1" />
</LinearLayout>

To fix this, change the layout_width's to 0dp for the Layouts with weights, everything else looks fine. Any time you use a weight, set the width or height (whichever one is determined by the weight) to 0dp.

in Android we have veryy good concept of Weight.
we give Weight To only in LinearLayout.
and We give Weight To the Layout Horizontally as well as vertical means in simple way we give weight layout height and Width.
in Weight You want To give a 0dp for the height or width which you want to give a weight.if you not give 0dp than its not affect to layout.
Widht wise Weight...
give 1 1 weight so textview takes 50 %and edittext 50% of total width of parent layout.
give 1 2 weight so textview takes 33.33% and editext takes 66.66% total width of parent layout.
give 1 3 weight so textview takes 25% and edittext takes 75% total width of parent layout.
Height wise Weight....
give 1 1 weight so textview takes 50% and edittext 50% of total Height of parent layout.
give 1 2 weight so textview takes 33.33% and editext takes 66.66% total Height of parent layout.
give 1 3 weight so textview takes 25% and edittext takes 75% total Height of parent layout.
in your case You want Width 0dp and weight is 1.
**You just add this code**
android:layout_weight="1"
android:layout_width="0dp"
below the code
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="BOM ID"
android:id="#+id/tvBOMID" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/etBOMID"
android:hint="BOM ID"
android:layout_weight="1" />
</LinearLayout>

just change :
android:layout_width="wrap_content"
to :
android:layout_width="fill_parent"
in textview and edittext.

Related

Understanding android:layout_weight

Why the following listing shows only the second TextView (red)?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="11111"
android:background="#00FF00" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0"
android:background="#FF0000"
android:text="00000"/>
</LinearLayout>
I know that if I set android:layout_height="0px" it will only show the first TextView (green), and I understand this behavior.
But why when I set android:layout_height="match_parent", the first TextView disappear completely from the screen.
From https://developer.android.com/guide/topics/ui/layout/linear.html
It disappers because it second one acquires full space as it is given
android:layout_weight="0"
and
android:layout_height="match_parent"** as mentioned in above link.
LinearLayout also supports assigning a weight to individual children
with the android:layout_weight attribute. This attribute assigns an
"importance" value to a view in terms of how much space it should
occupy on the screen. A larger weight value allows it to expand to
fill any remaining space in the parent view. Child views can specify a
weight value, and then any remaining space in the view group is
assigned to children in the proportion of their declared weight.
Default weight is zero.
For example, if there are three text fields and two of them declare a
weight of 1, while the other is given no weight, the third text field
without weight will not grow and will only occupy the area required by
its content. The other two will expand equally to fill the space
remaining after all three fields are measured.
If the third field is then given a weight of 2 (instead of 0), then it
is now declared more important than both the others, so it gets half
the total remaining space, while the first two share the rest equally.
android:layout_weight="1" means you are assigning the remaining space which is not occupied by other views to that view.. so here in your case second TextView is match_parent so no space is left blank thats why first TextView is not visible
P.S: Pass weightsum to the parent layout and distribute that weight among child Views according to your need.
Thanks
android:layout_weight means how u want these views to be showed comparing to each other and the parent view
for exampel, if u these codes:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"/>
</LinearLayout>
the result will be this image
if i change android:layout_weight of Button1 from 1 to 2 while Button2's weight is still 1, then Button1's Width will be 2times bigger than Button2's
Your code is basically saying, give my first text view all the space and my second text view none, try giving the children each a weight of 1, this should give them equal spacing. Also as you have a vertical oriented parent, setting the layout height in the children to match the parent is contradicting to the weight attribute. So remove it and replace with wrap content.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="11111"
android:background="#00FF00" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF0000"
android:text="00000"/>
</LinearLayout>
https://developer.android.com/guide/topics/ui/layout/linear.html
https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
https://ugia.io/2012/01/19/android-linearlayout-distribution-explained-weight-and-sizes/
match_parent is different from wrap_content or specific sizes(0px, 1px, 100px etc.), it will take all space of parent view till the size of LinearLayout in your case.
layout_weight is for balancing, and it depends on your layout_width
But why, if I set in the second TextView:
android: layout_weight = "0.1"
the second component of the TextView will take less space than if I set:
android: layout_weight = "0"
And if I set in the second TextView
android: layout_weight = "0.2"
the second component of the TextView will take even less space than if I set
android: layout_weight = "0.1"
So if I set higher the value of android:layout_weight, that it occupies a smaller size.
You have set the weight value of the first button to 1 and the second to 0,
so button one will cover the whole space
You should give a weight total to the parent layout and distribute the total between the children according to the desired proportion.

Horizontal LinearLayout with max width TextView

I have a LinearLayout with horizontal orientation and two TextView children. My goal is to have the left TextView expand to fit its text content, but only up to a maximum width (to leave room for the right TextView). The key is I want to be able to express this maximum width in terms of a percentage similar to the way layout_weight is specified.
The problem with setting the layout_weight is that the TextView is then unconditionally expanded to fill the maximum width, even if the contents don't require the extra space.
Here's my current layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/subject"
android:textSize="18sp"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.8"/>
<TextView android:id="#+id/date"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Setting a weight to one view only will only expand it to fill the space. If you want it to be relative you have to give the other view also weight in the layout.

How do I set the width of a child views to be divided amongst eachother?

I'm trying to create a LinearLayout with the left view width being 80% of its parents width and the second view taking up the remaining space. How do I accomplish this?
Trying using android:layout_weight when defining your child Views.
For view 1 use a layout weight of 0.8 and view 2 use a layout weight of 0.2
Example xml would be :
android:layout_weight=".8"
Like this:
Don't forget to set the width to 0.
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".8"
android:background="#color/blue" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".2"
android:background="#color/red" />

In android layouts what is the effect/meaning of layout_height="0dip"

I've seen several examples that use
android:layout_height="0px" or "0dip" but i do not understand the impact of this. It seems that would make the layout 0 pixels tall. Is the value mitigated but some other factor like 'weight' or the height of any parent views?
Yep you are right about the weight, when you want the width or height to be controlled by weight its convention to set that value to 0dip and let the weight control the actual value. Although I am pretty sure 0 is just arbitrary here you could put anything but putting 0 makes your intention more clear.
When using a LinearLayout if you set the layout_weight to a non-zero value and set the layout_height (or layout_width) to 0px or 0dip then the LinearLayout distributes any unassigned space along the appropriate axis based on the weights. So for example, if you look at the layout below the View with id *gestures_overlay* it has layout_height 0dip and layout_weight 1 so the parent LinearLayout stretches it to fill the available vertical space between the 2 surrounding LinearLayouts. If there was another View with the same 0dip layout_height and a layout_weight value then they would share the vertical space based on their weight values.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:text="#string/prompt_gesture_name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/gesture_name"
android:layout_width="0dip"
android:layout_weight="1.0"
android:layout_height="wrap_content"
android:maxLength="40"
android:singleLine="true" />
</LinearLayout>
<android.gesture.GestureOverlayView
android:id="#+id/gestures_overlay"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:gestureStrokeType="multiple" />
<LinearLayout
style="#android:style/ButtonBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/done"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="false"
android:onClick="addGesture"
android:text="#string/button_done" />
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="cancelGesture"
android:text="#string/button_discard" />
</LinearLayout>
</LinearLayout>
A relevant example from the official developer docs (http://developer.android.com/guide/topics/ui/layout/linear.html):
Equally weighted children
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1".
When we have to assign equal weight to the different view in LINEAR LAYOUT
then we assign either layout/width = 0dp(for horizontal orientation) or layout/height = 0dp(for vertical orientation) and set View /weight ==1 for every view inside that Linear Layout.
Advantage::::
-- on assigning width or height to 0dp then it have no impact and due to weight==1 all the view occupies same space and covered the whole screen size.

How does android:layout_weight work?

When I have the following, it shows top layout with four colors has much smaller area than the bottom layout area.
According to this documentation, when you add more to layout_weight, it should increase the area, but it decreases in the code.
<?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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4">
<TextView
android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="3"/>
<TextView
android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="row one"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row two"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row three"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row four"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
I had the same problem. The trick is not to use "wrap_content" or "fill_parent" for the controls you are setting a weight to. Instead set the layout_height to 0px (when inside a vertical layout) and then the child controls will get proportioned per the weight values.
If you get the error as
error
Suspicious size: this will make the view invisible, probably intended
for layout ...
remember to set the correct parameter on
android:orientation in parent
If you are using fill_parent in a LinearLayout the layout will take as much space as possible and all layout items defined later will have to deal with the space left.
If you set the height of both of you LinearLayouts to wrap_content the weight should work as documented.
I know this is late but hopefully it helps people:
Use android:weightSum on the parent. Here is a link to the dev docs.
http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum
Use android:weightSum of 1.0 on the parent and then use 0.x on the child views with android:layout_weight in order for them to use that ratio of space.
Building on what Janusz said, if you use fill_parent, you can then set android:layout_weight to "split" the "full area" between multiple layout items.
The layout_weight doesn't increase the area, it increases it "right" to the area. but it's also relative to the parent. If you have a parent with a layout_height=fill_parent, with a layout_weight=0 and the parent has a sibling with the same, setting layout_weight=1 to one of the children does not affect the parent.
Both the parent, and the sibling, would take up 50% of the available area that they can fill.
The solution is to set layout_height to 0px when you use layout_weight.
But why do you observe this apparently strange/inversed behavior ?
Shortly : the remaining space is negative and so the child with weight 4 receive more negative space and it's height is more reduced.
Details :
Assume that the height of your parent vertical layout is 100 pixels.
Layout height on each child is fill_parent (i.e. each child is also 100 pixels height)
The total height of all child = 2*100 pixels
The remaining height = 100 - (2*100) = -100 pixels (it is negative)
Now, let's distribute this remaining height between child. The first one will receive the biggest part : 80% (i.e. -100*4/(4+1)). The second child receive 20% (i.e. -100*1/(4+1))
Now compute the resulting height :
child 1 : 100 + (-80) = 20px
child 2 : 100 + (-20) = 80px
Nothing strange here, only mathematics. Just be aware that remaining space can be negative ! (or set the height explicitly at 0 as it is recommended)
In linear layout properties change the layout width to "fill_parent" instead of "wrap_content"
hope it helps.
If the orientation of linearlayout is vertical,then set layout_height as 0dp. In case of horizontal layout set layout_width as 0dp.
If we do not follow above rules,the view tends to take up space as per specified attributes and hence the alignment is not as per expectation.

Categories

Resources