UI Layouts Overview (Java)

In Android, layouts define how UI elements (Views) are arranged on the screen. Layouts are containers (subclasses of ViewGroup) that manage their child views’ size, position, and relationships. Understanding layouts deeply is crucial to designing scalable, responsive UI.

Key Layout Types

LinearLayout

Stack children horizontally or vertically.

RelativeLayout

Position relative to each other or parent.

FrameLayout

Single child or overlapping children (stack).

ConstraintLayout

Flexible, relative positioning with constraints.

TableLayout

Rows and columns like a grid.

ScrollView

Enable vertical scrolling of child content.

Comparison Table

LayoutBest ForDrawbacks
LinearLayoutSimple vertical/horizontal stackingDeep nesting for complex UI → slower performance
RelativeLayoutUI with relative positions (left/right/top)Deprecated in favor of ConstraintLayout (less flexible)
FrameLayoutOverlays (e.g., image with text)Not good for structured multiple children
ConstraintLayoutComplex, responsive UI with flat hierarchyLearning curve, more verbose XML
TableLayoutTabular data like formsRigid, not responsive by default
ScrollViewContent longer than screenCan only have 1 direct child

Micro-Level Breakdown

LinearLayout

LinearLayout (vertical)
TextView
Button
<!-- res/layout/activity_main.xml -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:text="Hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:text="Click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

Deep Detail: orientation defines stacking direction. Each child can use layout_weight to divide available space proportionally.

RelativeLayout

RelativeLayout
TextView (alignParentTop)
Button (below TextView)
<RelativeLayout ... >
  <TextView
      android:id="@+id/tv"
      android:layout_alignParentTop="true"
      android:text="Title"/>

  <Button
      android:layout_below="@id/tv"
      android:text="Next"/>
</RelativeLayout>

Deep Detail: Children use rules like below, toRightOf, alignParentStart. Flexible but replaced by ConstraintLayout for performance.

FrameLayout

FrameLayout
ImageView
TextView (over image)
<FrameLayout ... >
  <ImageView android:src="@drawable/bg"/>
  <TextView android:text="Overlay"/>
</FrameLayout>

ConstraintLayout

ConstraintLayout
Button (centered)
TextView (below button)
<androidx.constraintlayout.widget.ConstraintLayout ... >

  <Button
      android:id="@+id/btn"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintEnd_toEndOf="parent"/>

  <TextView
      app:layout_constraintTop_toBottomOf="@id/btn"
      app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Deep Detail: Constraints allow relative positioning to parent or siblings. Use Guideline and Barrier for adaptive UIs.

TableLayout

<TableLayout ... >
  <TableRow>
    <TextView text="Name"/>
    <EditText />
  </TableRow>
  <TableRow>
    <TextView text="Age"/>
    <EditText />
  </TableRow>
</TableLayout>

Good for forms. Rows auto-expand columns. But less flexible for modern responsive design.

ScrollView

<ScrollView ... >
  <LinearLayout android:orientation="vertical">
    <TextView ... />
    <ImageView ... />
    <!-- Many children here -->
  </LinearLayout>
</ScrollView>

Deep Detail: ScrollView only supports one direct child. Nest a LinearLayout/ConstraintLayout to hold multiple elements.

Performance & Best Practices

  • Minimize nesting: Deep nested layouts → slower measure/layout passes. Use ConstraintLayout to flatten.
  • Use wrap_content: For elements with fixed size; avoid match_parent in ScrollView children (will expand infinitely).
  • Tools: Use Layout Inspector & Hierarchy Viewer in Android Studio to debug layout performance.

Practice Tasks

  1. Create a login screen using LinearLayout. Replace it with ConstraintLayout → compare XML size.
  2. Create a profile card with FrameLayout (background image + text overlay).
  3. Use TableLayout for a registration form.
  4. Put 20 TextViews inside ScrollView and confirm it scrolls smoothly.