layout_weight and weight sum in android

layout_width and layout_height are the properties used in all the views as well as view groups . The purpose of these properties is to provide length and height to the view as well as the container that is view group. Lets understand it more clearly by the help of an image.

Android layout_wit and layout_height

layout_width 

 The area of view from left to right shows layout_width or the horizontal part of a view or view group is the layout_width.In other words, we can also say that the length of the view is the layout_width. It’s up to us what width we want to assign to a layout.

we can assign value to the layout_width Attribute in many ways:

1.wrap_content

2.match_parent

3.any size for example : 45dp

Android layout_width
Image 1

In the above image 1, the blue color rectangle is depicting a view group and the rectangle with orange color inside view group is a view. And the horizontal part of that view is showing layout_width.Let us consider one by one all the values of layout_width attribute.

layout_width with value

wrap_content

The value of an attribute,layout_width=”wrap_content” means that the width of the view or the view group will be just as much as the size of the content. As the word itself says, wrap the content no more extra space than that of the content.

Android wrap_content

Lets see how layout_width="wrap_content" looks in Real device with an example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am Wrap content"
android:textColor="#fff"
android:layout_marginRight="5dp"
android:textSize="45dp"
android:background="#455"/>

</LinearLayout>

match_parent

The value of an attribute,layout_width=”match_parent” means that the width of the view or the view group will be just as much as the size of the parent.

Here parent size means that the size of the container or view group. As the word itself says, match_parent which means the width will be as much as the width of parent that is view group.

Android match_parent

For Example

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="I am Match parent"
android:textColor="#fff"
android:layout_marginRight="5dp"
android:textSize="45dp"
android:background="#455"/>

</LinearLayout>

any size

The value of an attribute,layout_width=”20dp” means that the width of the view or the view group will be 20dp.

we can dynamically assign a value to any view or view group by providing it any size we want.

Android any_size

For Example

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="any size"
android:textColor="#fff"
android:layout_marginRight="5dp"
android:textSize="45dp"
android:background="#455"/>

</LinearLayout>

Leave a Comment