What's the difference between fill_parent and wrap_content?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
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.
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:
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.
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_parentin new code makes the XML look older than it needs to be; usematch_parentinstead. - Expecting
wrap_contentto ignore parent constraints causes confusion, especially inConstraintLayoutwhere constraints still limit measurement. - Replacing every
wrap_contentwithmatch_parentoften creates bloated layouts with too much empty space. - Confusing
match_parentwith weighted distribution inLinearLayoutleads to broken sibling sizing. - Forgetting that text can grow means
wrap_contentviews may become larger than expected when localization or accessibility font scaling is applied.
Summary
- '
wrap_contentsizes a view to its content.' - '
fill_parent, nowmatch_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_weightfor proportional space sharing; do not usematch_parentas a substitute.
Related reading
- What's the difference between setWebViewClient vs. setWebChromeClient?
- What's the difference between src/androidtest and src/test folders?
- What's the difference between the various methods to get an Android Context?
- What's the difference between 'weak' and 'assign' in delegate property declaration
- What's the dSYM and how to use it? iOS SDK
- What's the Kotlin equivalent of Java's String?
- What''s the point of NSAssert, actually?
- what's the purpose of double question mark in swift
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.