[Lesson 6]Layouts in Android – Part 1

Before studying the layout, you need to understand about basic attributes in all view group in android

  1. Width, Height of View : fill_parent (deprecated and renamed MATCH_PARENT in API Level 8 and higher), match_parent, wrap_content

Width wrap_content: which means that the view wants to be just big enough to enclose its content (plus padding)

Width match_parent: the view automatically fills to fit its parents’ size

attribute_wrapcontent_matchparent

2. Padding, Margin

Padding is the space inside the border, between the border and the actual view’s content
Margins are the spaces outside the border, between the border and the other elements next to this view.
padding_margin
3. Gravity , Layout_gravity
android:gravity is the Inside gravity of the View. The android:gravity is really an attribute of the View Group. It controls the way the contents of the View Group will be positioned horizontally and vertically.

gravity
android:layout_gravity is the Outside gravity of the View. That means, sets the gravity of the View or Layout in its parent.

Horizontal Linear Layout is a view group that aligns all its children in a single direction, horizontally.
For a horizontal Linear Layout the following values make sense:
  • top
  • center
  • bottom

layout_gravity_horizontal1

Vertical Linear Layout is a view group that aligns all its children in a single direction, vertically
For a vertical Linear Layout the following values make sense:
  • left
  • center
  • right

layout_gravity_vertical1

4. Layout weight

layout_weight layout_weight specifies how much of the extra space in the layout to be allocated to the View.
layout_weight is used in LinearLayouts to assign “importance” to Views within the layout. All Views have a default layout_weight of zero, meaning they take up only as much room on the screen as they need to be displayed. Assigning a value higher than zero will split up the rest of the available space in the parent View, according to the value of each View’s layout_weight and its ratio to the overall layout_weight specified in the current layout for this and other View elements. 

5 . View Identification (@+id, @id)

Any View object may have an integer ID associated with it, to uniquely identify the View within the tree. The syntax for an ID, inside an XML tag is: android:id=”@+id/{name_of_id}”

The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource.

The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file).

When referencing a resource ID, you do not need the plus-symbol, like so:

android:layout_toLeftOf=”@id/{id_of_reference_element}”

Leave a comment