Tuesday, 17 January 2017

Android Linear Layout

Android Linear Layout is an view group that arranges its views either in horizontal or vertical direction.The direction of views can be set with help of orientation property of linear layout in xml file or programmatically.

We can also set weightsum to linear layout and then can divide this weight to views so that views can occupy that much space in parent linear layout.We can also set gravity to views.

Here are some android linear layout attributes -


Attributes & Description
1
android:id
This is the ID which uniquely identifies the layout.
2
android:baselineAligned
This must be a boolean value, either "true" or "false" and prevents the layout from aligning its children's baselines.
3
android:baselineAlignedChildIndex
When a linear layout is part of another layout that is baseline aligned, it can specify which of its children to baseline align.
4
android:divider
This is drawable to use as a vertical divider between buttons. You use a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
5
android:gravity
This specifies how an object should position its content, on both the X and Y axes. Possible values are top, bottom, left, right, center, center_vertical, center_horizontal etc.
6
android:orientation
This specifies the direction of arrangement and you will use "horizontal" for a row, "vertical" for a column. The default is horizontal.
7
android:weightSum
Sum up of child weight
Here is an example of android linear layout we are creating an login screen in which we will have two edit text to enter user name ,password and two button one is to login and other is to get forgotten password.

Here is the code to create an linear layout using an xml file-


In the above code we have created one linear layout as root view its property gravity is set to center so that its contain can be in center of screen. We have given the orientation as vertical hence views will lie in vertical direction to each other.

Then there are two edit text views one of them is getting user name and other is taking password as input. Then we have created one linear layout to contain two button i.e. login and forgot password button.This linear layout has weightsum property having a value of 2 and this weightsum is divided into its views .

As you can see in the image that both button has width property value as 0 because when we define the weight property if its parent layout orientation is horizontal then its child view will have width property value as 0 and if linear layout orientation is vertical then child views height value will be zero.Here we have divided linear layout in two equal parts to provide equal space to buttons on screen.

And we have set the other properties like margin and hint text etc. so that layout looks better on screen otherwise there will be no space between views.It's output preview is shown in below image -


This is all about how to create Android Linear Layout in xml if you have any question regarding this please leave it in to comments.





No comments:

Post a Comment