15 49.0138 8.38624 1 0 4000 1 http://codeisall.com 300
Android Layouts

Android Layouts

Android Linear Layout

Linear layout arranges all GUI objects in a single direction either horizontally or vertically. It depends on how we define the orientation attribute. All children are arranged one by one. Because of that, a vertical list will only have one child per row, while horizontal list has multiple children in a row.

Attributes :

android:gravity – Defines the position of contents in x and y-axes. Values: left, right, top, bottom, center etc.

android:orientation – It defines a direction of arrangements of GUI objects. The default value is horizontal. Values: horizontal, vertical.

android:margin – Used to set margin in all alignments like left, right, top, bottom.

you can add android:orientation attribute to your LinearLayout. If you want to get all GUI objects in a row then a value for orientation must be horizontal. And if you want to get all GUI objects in a column then the value for orientation must be vertical. You can also modify orientation at the run time. In order to do that you can use setOrientation().

Example Of Android Linear Layout

Outputs For Both Horizontal & Vertical Orientation.

Android Linear Layout  Android Linear Layout

Android Relative Layout

With the help of Android relative layout, GUI objects specify their position relative to each other. All GUI objects are rendered as per given the order. For example, suppose we arrange the first element in the center of the screen, then other elements automatically aligned themselves to that element will be aligned relative to screen center. Android Relative layout puts widgets based on their relationship to other widgets in the container and the parent container.

Attributes :

android:layout_alignParentTop : The value must be true or false. If true then GUI object’s top should align with the top of the container.

android:layout_ alignParentBottom – The value must be true or false. If true then GUI object’s bottom should align with the bottom of the container.

android:layout_alignParentLeft – The value must be true or false. If true then GUI object’s left side should align with the left side of the container.

android:layout_ alignParentRight – The value must be true or false. If true then GUI object’s right side should align with the right side of the container.

android:layout_above – Indicates that the GUI object should be placed above the GUI object referenced in the property.

android:layout_below – Indicates that the GUI object should be placed below the GUI object referenced in the property.

android:layout_toLeftOf – Defines that the GUI object should be placed to the left of the widgets referenced in the property.

android:layout_toRightOf – Defines that the GUI object should be placed to the right of the widgets referenced in the property.

Example Of Android Relative Layout

Output

Android Relative Layout

Android Frame Layout

Android Frame layout basically a blank space on the screen. Later you can fill with the single GUI object. It means frame layout holds only one child view. All child elements of the frame layout are arranged to the top left corner of the screen, you can not specify a particular location for a child view.

Example Of Android Frame Layout

Output

Android Frame Layout

 Android Grid View

Android Grid View displays items in two-dimensional scrolling grid with rows and columns. Items in the grid view are automatically inserted to the layout using ListAdapter.

Attributes:

android:id – Unique id

android:columnWidth – Defines width for each column.

android:horizontalSpacing – Defines the default horizontal spacing between columns.

android:verticalSpacing – Defines the default vertical spacing between rows.

Example Of Android Grid View

activity_main.xml

MainActivity.java

Output

Android Grid View

Android Table Layout

Android Table layout arranges its GUI objects into rows and columns. Child views of a table layout are TableRow objects. One TableRow defines a single row in a table and each row has any number of columns. Each cell holds one View object. Android Table-layout does not display borders.

Example Of Android Grid View

Output

Android Table Layout

Share:
PREVIOUS POST
Android Overview
NEXT POST
Android Login Screen Design