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
Imagine a vertical Linear Layout that has two Textviews with weights 1 and 1, respectively. Then it will look like the following.
Now, if we assign Textview Alpha a weight of 1 and Textview Bravo 0.2, then it will look like this.
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.
When we set weight for one View and not for another, it looks like this.
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.
When we assign weights to two Views and one View is set to 0 then, it looks like this.
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.