Android Layout Notes

Basic Layouts

  • Nesting layouts negatively impacts performance
  • Using relative layouts is a good way to avoid using nested layouts

Linear Layouts

Relative Layouts


  • use layout gravity to vary the postions

Table Layout

  • like a linerear layout, but with each row nested like a relative layout

Basic Attributes


  • match_parrent - means that it wants to take as much width or height as the parrent does, which is the whole screen
android:text="some text">
  • wrap_content - will only take as much space as the item needs. eg: the snippet below will only need to take up the room for "some text"
android:text="some text">
  • wrap_content - if wrap_content was set to both the width and the height, then the element would only take as much space as it needs

  • android:background - Change the background color. Useful tequnique for determining how much space elements are taking up. eg: change the background to blue: android:backgroud="#9cf"

  • 50dp - can give a specific height or width. DP = Density-independed pixel to deal with devices with different screen densities. Use DP so that when you run on a double densisty device, it will use 2px

.5 - ldpi
1.0 mdpio
1.5 hdpi
2.0 xhdpi  (more pixles for physical area)

Margin vs padding

|  ---------------------------------
|  | /// Padding ///////////////////|
|  |  -------------------------------
|  |  |// Content ///////////////////
|  |--|------------------------------

  • //// = inside the view
  • margin android:layout_margin - defined in relation to the parent. mnargin is outside of the view
  • padding android:padding stays within the view itself


  • android:layout_gravity - gravity in relation to it's parrent

  • ** gravity** - gravity within the view itself

  • android:layout_gravity="center_horitizontal" will position the whole view in the center of the screen

  • android:gravity="center_horitizontal" in the case of a string inside a view, it will position the string in the center, but keep the view in the top left of the screen.

  • multiple gravity android.gravity="center_horizontal|bottom" This centers it within the view horitizontally and at the bottom of the view.

  • ** using gravity and layout_gravity together** eg: will put the view on the right side, with the text element inside at the bottom center


Which Layout to Use

Hierarchy Viewer

  • tool to show all of the views that are rendered on the screen so you can look at the views and properties

Different Layouts

1 dimension

Linear Layout

  • X and Y access - put views vertically or horizontally in a row

Frame Layout

  • Z access - can overlap views

2 dimensions

Relative Layout

  • put views relative to each other

Table Layout

  • good for aligning columns
  • take data that are different widths and put them together into columns that line up


  • Important when using a layout repeatedly i.e.: in a ListView
  • less views the better
  • less measurments the better. android goes through 3 phases: 1. measure -> 2. layout -> 3. draw