Android Development
Programming
XML Layout
UI Design
Coding Concepts

What is the difference between match_parent and fill_parent?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In the world of Android development, managing how views are sized and positioned within a parent container is crucial for creating visually appealing and functionally precise user interfaces. Two commonly encountered terms in this context are match_parent and fill_parent. While they might seem to serve similar purposes at first glance, it's important to understand their definitions, usage, and historical context.

Understanding match_parent and fill_parent

Both match_parent and fill_parent are constants used in Android layout XML files to specify the size of a view or layout. They instruct the view to expand its dimensions to match or fill the dimensions of its parent container. Technically, both achieve the same result in that they make the child view size equal to the parent view size. However, there is a key distinction in their usage based on Android API levels.

  • fill_parent: This was the original term used in earlier versions of Android (API Level 1). It tells the view to become as big as the parent view, thereby filling the space of the parent.
  • match_parent: Introduced in Android API Level 8 (Android 2.2), match_parent replaced fill_parent and means exactly the same thing—to make the view’s size match the size of its parent container. The renaming was done to make the XML attribute name more descriptive and accurate as to its effect.

Despite fill_parent being deprecated since API Level 8, it still functions as intended and is equivalent to match_parent. However, using match_parent is recommended for better code clarity and future compatibility.

Why the Change from fill_parent to match_parent?

The decision to move from fill_parent to match_parent by the Android development team was to more accurately reflect what the property does. The term “fill” might imply filling content, which can be ambiguous, whereas “match” clearly indicates that the size should match the parent’s size.

Examples of Usage in XML Layouts

Here’s a simple example to illustrate how match_parent and fill_parent can be used interchangeably, though match_parent is preferred.

xml
1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2    android:layout_width="match_parent"
3    android:layout_height="match_parent"
4    android:orientation="vertical">
5
6    <Button
7        android:layout_width="match_parent"
8        android:layout_height="wrap_content"
9        android:text="Submit" />
10</LinearLayout>

The LinearLayout will match the size of its parent (typically the full screen unless nested inside another container), and the Button will match the width of the LinearLayout while only being as tall as its content (“wrap_content”).

Impact on UI Design

Using match_parent (or fill_parent in older code) has significant implications for UI design:

  • Ensures consistency across different device screen sizes by dynamically adjusting the view size.
  • Helps avoid complex calculations for dynamic sizing in code, simplifying layout design.
  • Essential for responsive design, ensuring that UI components expand appropriately on both smaller and larger screens.

Recommendations

Developers are encouraged to use match_parent for new projects and when maintaining or updating existing code, replace fill_parent with match_parent to improve readability and ensure compatibility with future Android versions.

Summary Table

AttributeAPI IntroducedDescriptionCurrent Recommendation
fill_parentAPI Level 1Expands the view to the size of its parent containerDeprecated
match_parentAPI Level 8Expands the view to the size of its parent containerRecommended

In conclusion, while match_parent and fill_parent can be used interchangeably to expand a view to match the size of its parent, match_parent is the preferred choice. It reflects a more intuitive understanding of its function, aligning with modern Android development practices. The switch to match_parent represents a broader trend in software development towards clearer and more descriptive coding practices.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.