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
| Layout | Best For | Drawbacks |
|---|---|---|
| LinearLayout | Simple vertical/horizontal stacking | Deep nesting for complex UI → slower performance |
| RelativeLayout | UI with relative positions (left/right/top) | Deprecated in favor of ConstraintLayout (less flexible) |
| FrameLayout | Overlays (e.g., image with text) | Not good for structured multiple children |
| ConstraintLayout | Complex, responsive UI with flat hierarchy | Learning curve, more verbose XML |
| TableLayout | Tabular data like forms | Rigid, not responsive by default |
| ScrollView | Content longer than screen | Can only have 1 direct child |
Micro-Level Breakdown
LinearLayout
<!-- 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
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 android:src="@drawable/bg"/>
<TextView android:text="Overlay"/>
</FrameLayout>
ConstraintLayout
<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
- Create a login screen using LinearLayout. Replace it with ConstraintLayout → compare XML size.
- Create a profile card with FrameLayout (background image + text overlay).
- Use TableLayout for a registration form.
- Put 20 TextViews inside ScrollView and confirm it scrolls smoothly.