What is the difference between match_parent and fill_parent in Android ?


Introduction

An android application consists of different types of views within it such as image view, text view, buttons and many others. For this different type of view we have to specify the size of that view to be displayed on the screen. We can provide a custom size for that view to take a specific height as well as width. But if we want to provide size to these views so that they can be changed for different screen sizes can be easily handled. We can provide width and height for these views as match_parent or fill_parent. Adding match_parent and fill_parent will help you to align the specific view for different screen sizes. In this article we will take a look at What is the difference between match_parent and fill_parent in Android.

What is meant by match_parent?

Match_parent is an attribute used to define the width or height of a view to be equal to the width or height of its parent view. This means that the view will take up the same space as its parent. This is often used when creating a layout with multiple views, as it ensures that the views are of equal size. For example, if we specify a width for any text view as match_parent, then the view will expand to the entire width of the screen and it will also align for different screen sizes, similarly if we specify the height as match_parent it will take the complete height of the screen from top to the bottom.

This attribute is given in the size parameter. Below is the simple example of creating a text view by specifying the width and height as match_parent.

<!-- creating a text view for displaying a text message with match_parent-->
<TextView
   android:id="@+id/idTVMessage"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center"
   android:text="Hello World!"
   android:textAlignment="center"
   android:textSize="20sp"
   android:textStyle="bold" />

Explanation − In the above code we are specifying the width and height of the text view as match_parent which will take up this text view to take the complete area of the screen and the text will be aligned to the center of the screen as we are giving text alignment as center.

What is meant by fill_parent?

Fill_parent is similar to match_parent, but it is used for older versions of Android. The effect is the same, but the syntax is slightly different. This attribute was deprecated in API Level 8 (Android 2.2) and replaced with match_parent. For example, if we specify a width for any text view as fill_parent, then the view will expand to the entire width of the screen and it will also align for different screen sizes, similarly if we specify the height as fill_parent it will take the complete height of the screen from top to the bottom.

This attribute is given in the size parameter. Below is the simple example of creating a text view by specifying the width and height as fill_parent.

<!-- creating a text view for displaying a text message with fill_parent-->
<TextView
   android:id="@+id/idTVMessage"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:gravity="center"
   android:text="Hello World!"
   android:textAlignment="center"
   android:textSize="20sp"
   android:textStyle="bold" />

Explanation − In the above code we are specifying the width and height of the text view as fill_parent which will take up this text view to take the complete area of the screen and the text will be aligned to the center of the screen as we are giving text alignment as center.

What is the difference between match_parent and fill_parent?

There are a few key differences between match_parent and fill_parent. First, match_parent is the preferred choice, as it is more up-to-date and uses the same syntax as other attributes. Second, fill_parent is slightly more restrictive, as it only allows the view to take up the same space as its parent. Match_parent is slightly more flexible, as it allows the view to take up more space than its parent.

In terms of which attribute to use, match_parent is the preferred choice, as it is the more up-to-date version. However, if you need to support older versions of Android, then you may need to use fill_parent.

It is also important to note that match_parent and fill_parent should not be used interchangeably. If you use match_parent in an older version of Android, it will not have the desired effect. Likewise, if you use fill_parent in a newer version of Android, it will not have the desired effect.

In summary, match_parent and fill_parent are two attributes used to define the size of a view in Android. They have the same effect, but match_parent is the preferred choice as it is more up-to-date and uses the same syntax as other attributes. It is also important to note that they should not be used interchangeably, as the desired effect will not be achieved.

match_parent

fill_parent

It has been used since API level 8.

It has been used since API level 8.

It is equivalent to LayoutParams.MATCH_PARENT.

It is equivalent to LayoutParams.FILL_PARENT.

The value for width and height in layout will be set to the full dimension of its parent view's width or height respectively.

The value for width and height in layout will be set to fill up the remaining space as much as possible inside the parent view's container no matter what it means for inner elements sizes and positioning.

Conclusion

In the above article we have taken a look at what is meant by match_parent and fill_parent in Android and how we can use them within our application. Along with that we have also taken a look on Difference between both match_parent and fill_parent.

Updated on: 30-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements