Wednesday, January 30, 2013

Layouts in Android!!


Layouts in Android:

The UI in Android is a hierarchy of viewgroups and views. The viewgroups will be intermediate nodes in the hierarchy, and the views will be terminal nodes.
For example, in the above main.xml file, the LinearLayout is a viewgroup and the TextView is a view.
Android provides the following standard layouts (viewgroups) that can be used in you Android application.
  • LinearLayout
  • RelativeLayout
  • AbsoluteLayout
  • FrameLayout
  • TableLayout


LinearLayout:

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute.
All children of a LinearLayout are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high

RelativeLayout:

In a Relative layout every element arranges itself relative to other elements or a parent element.

In order to specify the position with relation to its parent container, we use android:layout_alignParentTop=“true” and android:layout_alignParentLeft=“true” to align elements to the top-left of the parent container. Then, to align with respect to another element we can use the properties android:layout_alignLeft=“@+id/otherelement” and android:layout_below=“@+id/otherelement”.

AbsoluteLayout:

In absolute layout, we can specify the exact coordinates of each control that we want to place. In absolute layout, we will give the exact X and Y coordinates of each control. You can move them around by changing the android:layout_x and android:layout_y properties within the control.
AbsoluteLayout has been deprecated and is not recommended , as it gets tied to a particular screen size.

FrameLayout:

Frame layout is used when you want to show one item on each screen. Using frame layout, we can have multiple items, but they will be overlapping and only only displaying themselves one at a time.

TableLayout:

A layout that arranges its children into rows and columns. A TableLayout consists of a number of TableRow objects, each defining a row.