Android Development, Simply Explained

How layout weights work in Android?

Providing weights to a view means assigning the importance of that view within a particular layout. It means that you specify which view should be given more space and which view should not.

How exactly does it work?

Now, assigning weights is not complicated at all. The greater the weight of a view the greater is the space occupied by the view. There are a few things to remember. Layout weights work only in case of Linear Layout. If the orientation of the layout is set to vertical then the layout height should be set to “zero” and if the orientation is set to horizontal then the 

Example 1:

Imagine a vertical Linear Layout that has two Textviews with weights 1 and 1, respectively. Then it will look like the following.

Layout Weight Screenshot(1)

Example 2:

Now, if we assign Textview Alpha a weight of 1 and Textview Bravo 0.2, then it will look like this.

Layout Weight Screenshot(2)


In the above example, you can see, when we assign lesser weight to a particular Textview it occupies less space in the layout. Here Alpha occupies more space as it’s weight is set to 1 and bravo occupies less space as its weight is set to 0.2.

Example 3:

When we set weight for one View and not for another, it looks like this.

Layout Weight Screenshot(3)

In the above example, Alpha is given a weight of 1 and Bravo and Delta are set to 0 or not given any weights. Here, Alpha occupies the maximum space while Bravo and Delta stay unchanged.

Example 4:

When we assign weights to two Views and one View is set to 0 then, it looks like this.

Layout Weight Screenshot(4)

In the above example, you can see that two Views that have been given weights divide the remaining white space among themselves while the View that has 0 weight stays unchanged. Here, Alpha is 1, Bravo is 2 and Delta are set to 0.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.