Android
Layout
XML
UI Design
Development

What's the difference between fill_parent and wrap_content?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Android layouts, wrap_content means a view should be only as large as its content needs, while fill_parent means it should expand to the size offered by its parent. In modern Android code, fill_parent has been replaced by match_parent, but the sizing behavior is the same.

What wrap_content Means

When a view uses wrap_content, Android measures the content first and chooses the smallest size that can display it properly. A short TextView with wrap_content usually becomes just wide enough for its text and just tall enough for one line or however many lines are needed.

xml
1<TextView
2    android:layout_width="wrap_content"
3    android:layout_height="wrap_content"
4    android:text="Hello" />

This is useful when the content itself should determine the size. Buttons, labels, icons, and badges often use wrap_content because taking extra space would not improve the UI.

What fill_parent or match_parent Means

fill_parent tells the child to grow to the full size made available by the parent on that axis. Newer code should use match_parent instead.

xml
1<TextView
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:text="Full width row" />

Here the TextView stretches across the width of its container, even if the text is short. That is useful for row items, separators, backgrounds, and controls that should align with container edges.

Parent Rules Still Matter

Neither setting exists in isolation. The parent layout decides how much space is available and how children are measured. In a vertical LinearLayout, a child with match_parent width usually fills the row width. In a ConstraintLayout, the final size also depends on constraints. In a scrolling container, unbounded measurement can produce different results again.

So the right mental model is not "always fill screen" versus "always hug content." The correct model is "take the offered parent size" versus "measure from content."

Common Layout Example

This example shows both values working together:

xml
1<LinearLayout
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:orientation="vertical">
5
6    <TextView
7        android:layout_width="wrap_content"
8        android:layout_height="wrap_content"
9        android:text="Section title" />
10
11    <Button
12        android:layout_width="match_parent"
13        android:layout_height="wrap_content"
14        android:text="Continue" />
15</LinearLayout>

The title sizes itself to the text, while the button stretches across the available width. That difference is intentional because the two controls communicate different visual roles.

Interaction with layout_weight

In LinearLayout, developers often confuse match_parent with weighted expansion. If you want siblings to share leftover space proportionally, layout_weight is the tool for that. match_parent alone does not divide remaining space between peers cleanly.

xml
1<LinearLayout
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:orientation="horizontal">
5
6    <Button
7        android:layout_width="0dp"
8        android:layout_height="wrap_content"
9        android:layout_weight="1"
10        android:text="Yes" />
11
12    <Button
13        android:layout_width="0dp"
14        android:layout_height="wrap_content"
15        android:layout_weight="1"
16        android:text="No" />
17</LinearLayout>

In this case, 0dp plus layout_weight is the correct pattern. Using match_parent for both buttons would not express the same intent.

Why fill_parent Was Deprecated

fill_parent was renamed to match_parent to make the behavior clearer. The view is not filling the universe; it is matching the size determined by its parent. Legacy XML still compiles in many projects, but new code should use match_parent for readability and consistency.

Common Pitfalls

  • Using fill_parent in new code makes the XML look older than it needs to be; use match_parent instead.
  • Expecting wrap_content to ignore parent constraints causes confusion, especially in ConstraintLayout where constraints still limit measurement.
  • Replacing every wrap_content with match_parent often creates bloated layouts with too much empty space.
  • Confusing match_parent with weighted distribution in LinearLayout leads to broken sibling sizing.
  • Forgetting that text can grow means wrap_content views may become larger than expected when localization or accessibility font scaling is applied.

Summary

  • 'wrap_content sizes a view to its content.'
  • 'fill_parent, now match_parent, sizes a view to the space offered by its parent.'
  • The parent layout still controls measurement, so the same child setting can behave differently in different containers.
  • Use layout_weight for proportional space sharing; do not use match_parent as a substitute.

Course illustration
Course illustration

All Rights Reserved.