1 of 15

Layouts

Prasanna Kumar K R

2 of 15

Layout

  • Layout defines the structure for a user interface in your app, such as in an activity.
  • All elements in the layout are built using a hierarchy of View and ViewGroup objects.
  • A View usually draws something the user can see and interact with. View objects are usually called "widgets"
  • ViewGroup is an invisible container that defines the layout structure for View and other ViewGroup objectsL
    • Layouts

3 of 15

4 of 15

Declare a Layout

  • Declare a layout in two ways:
    • Declare UI elements in XML.
      • Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts.
    • Instantiate layout elements at runtime.
      • app can create View and ViewGroup objects (and manipulate their properties) programmatically.

5 of 15

Load the XML Resource

6 of 15

Layout Parameters

  • XML layout attributes named layout_something

7 of 15

layout_width and layout_height

  • wrap_content tells your view to size itself to the dimensions required by its content.
  • match_parent tells your view to become as big as its parent view group will allow.

8 of 15

LinearLayout

  • It is a view group that aligns all children in either vertically or horizontally.
  • 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.
  • android:orientation
    • It is specifies the direction of arrangement and you will use "horizontal" for a row, "vertical" for a column.
    • The default is horizontal.

9 of 15

Linear Layout

<LinearLayout

android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >

.

.

.

.

</LinearLayout>

10 of 15

Relative Layout

  • Enables you to specify the location of child objects relative to each other or to the parent
  • android:gravity
    • It 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.

11 of 15

<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="16dp" android:paddingRight="16dp" >

.

.

</RelativeLayout>

12 of 15

Table Layout

  • Android TableLayout going to be arranged groups of views into rows and columns.

13 of 15

<TableLayout

android:layout_width="fill_parent" android:layout_height="fill_parent">

<TableRow android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="Time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1" /> <TextClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textClock" android:layout_column="2" />

</TableRow>

<TableRow> <TextView android:text="First Name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1" /> <EditText android:width="200px" android:layout_width="wrap_content" android:layout_height="wrap_content" />

</TableRow>

</TableLayout>

14 of 15

List View

  • It is a view which groups several items and display them in vertical scrollable list.
  • An adapter actually bridges between UI components and the data source that fill data into UI Component

15 of 15

GridView

  • It shows items in two-dimensional scrolling grid (rows & columns) and the grid items are not necessarily predetermined but they automatically inserted to the layout using a ListAdapter