Android Layout Weight

Linear Layout and weight in Android

You are not setting the layout_weight property. Your code reads weight="1" and it should read android:layout_weight="1".

What does android:layout_weight mean?

With layout_weight you can specify a size ratio between multiple views. E.g. you have a MapView and a table which should show some additional information to the map. The map should use 3/4 of the screen and table should use 1/4 of the screen. Then you will set the layout_weight of the map to 3 and the layout_weight of the table to 1.

To get it work you also have to set the height or width (depending on your orientation) to 0px.

Android Layout Weight distribution

Understand it using the below example

Weight defines how much space a view will consume compared to other views within a LinearLayout.

Weight is used when you want to give specific screen space to one component compared to other.

Key Properties:

  • weightSum is the overall sum of weights of all child views. If you don't specify the weightSum, the system will calculate the sum of all the weights on its own.

  • layout_weight specifies the amount of space out of the total weight sum
    the widget will occupy.

Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="4">

<EditText
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Type Your Text Here" />

<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Text1" />

<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Text1" />

</LinearLayout>

The output is:

Sample Image

Now even if the size of the device is larger, the EditText will take 2/4 of the screen's space. Hence the look of your app is seen consistent across all screens.

[This if before you edit your question might be irrelevant now

Now in your Bonfire at the beach there is no weight and its wrap_content so there is no grantee that it will take the remaining space! and that space can remain after adding it will differ with the screen size of device ]

Note:
Here the layout_width is kept 0dp as the widget space is divided horizontally. If the widgets are to be aligned vertically layout_height will be set to 0dp.

This is done to increase the efficiency of the code because at runtime the system won't attempt to calculate the width or height respectively as this is managed by the weight. If you instead used wrap_content the system would attempt to calculate the width/height first before applying the weight attribute which causes another calculation cycle.


Lets Move to your XML
see how i used them

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:weightSum="3" //<-------------------
android:layout_height="match_parent">

<ImageView
android:src="@drawable/mc"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:layout_weight = "1"/> //<-------------------

<TextView
android:text="You're invited!"
android:layout_width="match_parent"
android:layout_height="0dp"
android:textColor="@android:color/white"
android:textSize="54sp"
android:layout_weight = "1" //<-------------------
android:background="#009688" />

<TextView
android:layout_weight = "1" //<------------------- if you remove this , this text view will be gone cuz its 0 by default
android:text="Bonfire at the beach"
android:layout_width="match_parent"
android:layout_height="0dp"
android:textColor="@android:color/white"
android:textSize="34sp"
android:background="#009688" />

</LinearLayout>

Now you ask what if you have not given android:layout_weight ,Default weight is zero. Zero means view will not be shown, that empty space will remain there

Since you don't believe you can read the documentation
Sample Image


EDIT: 2

Since you said that, i went through android-visualizer that you use
and Have you ever noticed this...

"Line 6: The attribute android:weight_sum is not supported here."

thing on its bottom.

Meaning they are not providing that functionality to adjust your layout boundaries. Its just a simple online tool.I am
not saying it is not recommended to use, but my personal idea is, you
can't touch the depth of android if you use that.

Now if you want a confirmation what actually happens have a look on android studio/ eclipse as well which are read IDE s

This is android studio

Sample Image

Can you see any view contain your text "Bonfire at the beach"? no
Instead a.studio display a red line in XML.

Suspicious size: this will make the view invisible

Because there is no layout_weight is given and we have added 0 height

Now you can accept the answer :)

Android Layout Weight

It doesn't work because you are using fill_parent as the width. The weight is used to distribute the remaining empty space or take away space when the total sum is larger than the LinearLayout. Set your widths to 0dip instead and it will work.

Android layout-weight:How to explain the calculation of proportion when width set 'match_parent'

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.

Calculation to assign any Remaining/Extra space between child. (not the total space)

space assign to child = (child individual weight) / (sum of weight of every child in Linear Layout)

if any one of the children is given 0 weight then it takes up the minimum space required to render and rest of available space is distributed among the children by the above formula

The Reverse expansion takes place in your case since:
because you are using match_parent as the layout_width. The weight is used to distribute the remaining empty space,and your first element has match parent therefore it tries to occupy the highest space horizontally and remaining lesser space is distributed among two other children.

Again for the second child,it tries to expand to the maximum space available leaving a very less space for the last child,therefore expansion is totally reverse to the applied individual weights.

In other words,
match_parent dominates like a boss in this case.
No specific calculation appears here rather Priority to the children in order can be seen very clearly.

Android layout weight doesn't work

Change the layout width of your LinearLayout to match_parent or set a specific value for it.

Explain
If you use LinearLayout width = wrap_content, the width of LinearLayout will depend of the childs.

And you also set the weight for the childs, so the the width of the child will depend on the parent.

=> (width of parent depend on childs, width of childs depend on parent) then I think the View will don't know what to do

Why is the same layout_weight leading to different widths for Android ImageButton and Button?

You need to set android:layout_width="0dp" for all buttons that should be the same width. (More precisely, you need to set the same layout_width for all those buttons and 0dp is the conventional width to use. When using equal weights, as long as there's still extra space to distribute, the actual width you use isn't relevant to the result.) The layout_weight only determines how the remaining space is allocated after the buttons take up their assigned widths. So if they start out unequal (which they will with widths of wrap_content), they remain different after their weights are taken into account.



Related Topics



Leave a reply



Submit