Android layout


An Android layout defines the visual structure for a user interface. A layout is subclass of ViewGroup that provide an invisible container which can hold other View or/and ViewGroup.

In simple word, a Layout specify how it is going to organize View and ViewGroup in user interface.
An Android layout file can contain exactly one View or ViewGroup.

We can declare an android layout in two ways:

  • Declare UI elements in XML
  • Instantiate layout elements at runtime

Type of layout in android

There are 5 type of layout in android. These android layout types are:

  • Linear Layout: Android LinearLayout is a view group that organize all its children in a single direction, vertically or horizontally.

    See more on linear layout

  • Relative Layout: Android RelativeLayout is a view group that organizes child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).

    See more on Android RelativeLayout

  • Table Layout: Android TableLayout is a ViewGroup that displays child View elements in rows and columns. TableLayout positions its children into rows and columns. TableLayout containers do not display border lines for their rows, columns, or cells.

    See more on Android TableLayout

  • Grid Layout: Android GridLayout places its children in a rectangular grid.
    The grid is composed of a set of infinitely thin lines that separate the viewing area into cells.

    See more on Android GridLayout

  • Frame Layout: Android FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.

    See more on FrameLayout


Common xml attributes of android layout

Android layouts cantain some common xml attributes, and some xml attributes are specific to only those types of layout. Here are few common xml attributes that we use frequently:

Xml attributes Description
android:id android:id specify Id that uniquely identify that View/ViewGroup.
android:layout_width android:layout_width specify the width of View/ViewGroup.
android:layout_height android:layout_height specify the height of View/ViewGroup.
android:layout_marginTop android:layout_marginTop specify the extra space on the top side of the layout that will be left.
android:layout_marginBottom android:layout_marginTop specify the extra space on the top side of the layout that will be left.
android:layout_marginLeft android:layout_marginLeft specify the extra space on the left side of the View/ViewGroup that will be left.
android:layout_marginRight android:layout_marginRight specify the extra space on the right side of the View/ViewGroup that will be left.
android:layout_margin android:layout_margin specify the extra space on the top, bottom, right and left side of the View/ViewGroup that will be left.
android:layout_gravity android:layout_gravity specify how children will be positioned.
android:layout_weight android:layout_weight specify how much of the extra space in the layout should be allocated to the View.
android:layout_x android:layout_x specifies the x-coordinate of the layout.
android:layout_y android:layout_y specifies the y-coordinate of the layout.
android:paddingLeft android:paddingLeft specifies the left padding filled for the layout.
android:paddingRight android:paddingRight specifies the right padding filled for the layout.
android:paddingTop android:paddingRight specifies the top padding filled for the layout.
android:paddingBottom android:paddingBottom specifies the bottom padding filled for the layout.
android:padding android:padding specifies the top, bottom, right and left padding filled for the layout.

Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.