1431

I'm a little confused about two XML properties: match_parent and fill_parent. It seems that both are the same. Is there any difference between them?

Adam Stelmaszczyk
  • 18,835
  • 4
  • 63
  • 104
vnshetty
  • 19,471
  • 23
  • 60
  • 102
  • if you feel free https://developer.android.com/reference/android/view/ViewGroup.LayoutParams – Shomu Oct 13 '18 at 12:31

17 Answers17

1251

They're the same thing (in API Level 8+). Use match_parent.

FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)

...

fill_parent: The view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by match_parent.

http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html

Matt Ball
  • 332,322
  • 92
  • 617
  • 683
  • 5
    if I write app for both 1.6 and 2.2, I will use fill_parent for compatibility, is it right? – emeraldhieu Aug 02 '11 at 14:09
  • 13
    There must be some differences right? Otherwise why Google deprecated it and renamed the new one? – stuckedoverflow Jul 20 '12 at 04:59
  • 50
    @Halim No, there is **no differece**: Both are defined as constant `-1`. I you feel better about it, I don't get the impovement in that either... :-) – arpy Aug 03 '12 at 11:51
  • @arpy - Actually, there has to be some internal differences. There's an error that pops up on older (post-API 8 but pre ICS) that can appear when using MATCH_PARENT as a constant instead of FILL_PARENT when creating a view programatically. Error: "Can't get the viewWidth" – jlovison Dec 20 '12 at 06:53
  • 2
    @jlovison No, there are no differences. Both are `-1`. You get the error because the older platform does not know about the new name of the very same constant. – J.G.Sebring Jan 30 '15 at 09:17
  • 17
    It is so weird that Android Studio still puts "fill_parent" in all default template created layouts! I know they are the same, but I keep coming back to this SO question every year to make sure no one has discovered that there really is a difference. Can the Google/Android-team please change the default value from "fill_parent" to "match_parent"? Thanks in advance! :) – swooby May 21 '15 at 20:22
  • @Matt Ball, hi. Although it is an old post i will be happy to understand something instead of opening a new post that will be marked as "duplicated". Can you please explain me why it is minus padding and not minus margin? Padding "talks" about the gap inside a view (component), while margin talks about the gap between two components, so isn't is correct to say that the view (component) will take a place that is equal to his parent minus margin? – Eitanos30 Dec 08 '19 at 18:39
253

Google changed the name to avoid confusion.

Problem with the old name fill parent was that it implies its affecting the dimensions of the parent, while match parent better describes the resulting behavior - match the dimension with the parent.

Both constants resolve to -1 in the end, and so result in the identical behavior in the app. Ironically enough, this name change made to clarify things seems to have added confusion rather than eliminating it.

J.G.Sebring
  • 5,642
  • 1
  • 27
  • 42
  • 18
    ...except with RelativeLayout's child with width=match_parent and, say, leftOf another. It does not match its parent's dimension, it fills what remains in it. That serves only to CAUSE confusion. – kaay May 13 '13 at 05:52
  • 7
    As FILL_PARENT and MATCH_PARENT means that the view wants to be as big as its parent, minus the parent's padding isn't that the interior space rather than the outer dimensions? Now I'm even more confused! – Caltor Sep 03 '13 at 13:35
  • @bnieland I have removed the references to inner and outer dimensions as they don't agree with the google documentation at http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#FILL_PARENT although I can see a similar (unsubstantiated) quote at http://sevennet.org/2014/11/22/how-to-what-is-the-difference-between-match_parent-and-fill_parent/ which is where you might have got your information from. – Caltor Jan 29 '15 at 17:15
30

Functionally no difference, Google just changed the name from fill_parent to match_parent, from API level 8 (Android 2.2). FILL_PARENT is still available for compatibility reason.

LayoutParams.FILL_PARENT and LayoutParams.MATCH_PARENT both have value -1. Not sure what tempted google to change from Fill Parent to Match Parent :)

Since most of the phones are >= Android 2.2 .. you should use Match Parent for future compatibility... not sure when they will discontinue the older Fill Parent constant!

Umair
  • 1,187
  • 1
  • 12
  • 26
12

For compatibility sake it's better to stick to fill_parent, i.e, when supporting below API 8 devices. But if your app targets API 8 and upwards, you should use match_parent instead.

Olumide Oyetoke
  • 319
  • 4
  • 15
  • 8
    Only backwards compatibility though. If `FILL_PARENT` is deprecated (as mentioned by Matt Ball above), then the only option for forward compatibility is `MATCH_PARENT`. –  Jul 02 '12 at 15:51
  • 2
    Agree but if you have in plan to write code for lower API level devices ex: 2.3,2.2,2.1 for the moment you need to use FILL_PARENT. I had some problems using match_parent for older versions. – MSA Jun 30 '13 at 21:43
  • it's time to delete this answer. It's not relevant anymore and can give the wrong advise to new developers – Tim Mar 28 '18 at 14:23
9

FILL_PARENT is deprecated in API level 8 and MATCH_PARENTuse higherlevel API

Avanish Kumar
  • 2,078
  • 2
  • 22
  • 30
9

match_parent is used in place of fill_parent and sets it to go as far as the parent goes. Just use match_parent and forget about fill_parent. I completely ditched fill_parent and everything is perfect as usual.

Check here for more.

Koech
  • 405
  • 6
  • 5
7

Both have similar functionality only difference is that fill_parent is used up to API level 8 and match_parent is used after API level 8 or higher level.

Praveen Gaur
  • 211
  • 3
  • 6
7

When you set layout width and height as match_parent in XML property, it will occupy the complete area that the parent view has, i.e. it will be as big as the parent.

<LinearLayout
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:background="#f9b0b0">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#b0f9dc"/>

</LinearLayout>

Hare parent is red and child is green. Child occupy all area. Because it's width and height are match_parent.

enter image description here

Note : If parent is applied a padding then that space would not be included.

<LinearLayout
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:background="#f9b0b0"
    android:paddingTop="20dp"
    android:paddingBottom="10dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#b0f9dc"/>

</LinearLayout>

enter image description here

So TextView hight = 300dp(parent hight) - (20(paddingTop)+10(paddingBottom)) = (300 - 30) dp = 270 dp

fill_parent Vs match_parent

fill_parent is previous name of match_parent

For API Level 8 and higher fill_parent renamed as match_parent and fill_parent is deprecated now.

So fill_parent and match_parent are same.

API Documentation for fill_parent

The view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by {@code match_parent}.

6

Just to give it a name closer to it's actual action. "fill_parent" does not fill the remaining space as the name would imply (for that you use the weight attribute). Instead, it takes up as much space as its layout parent. That's why the new name is "match_parent"

AndroidGeek
  • 30,803
  • 14
  • 212
  • 262
  • 2
    I do not understand people's logic when they state this. Parent has padding. Or child is leftOf another. Is the child matching it? No. is it filling the remaining space? Here in what is apparently Bizzarro World, we answer: yes. – kaay Jan 28 '15 at 09:49
  • 1
    @kaay I totally agree the new name is worse than the old one. But what Google says goes. – Caltor Feb 04 '15 at 23:37
4

match_parent and fill_parent are same property, used to define width or height of a view in full screen horizontally or vertically.

These properties are used in android xml files like this.

 android:layout_width="match_parent"
 android:layout_height="fill_parent"

or

 android:layout_width="fill_parent"
 android:layout_height="match_parent"

fill_parent was used in previous versions, but now it has been deprecated and replaced by match_parent. I hope it'll help you.

Meenal
  • 2,854
  • 5
  • 17
  • 42
Mansuu....
  • 1,176
  • 13
  • 27
4

fill_parent: The view should be as big as its parent.

now this content fill_parent is deprecated and replaced by match_parent.

4

match_parent, which means that the view wants to be as big as its parent (minus padding).

wrap_content, which means that the view wants to be just big enough to enclose its content (plus padding)

For sake of better illustration, I have created a sample layout that demonstrate this concept. To see it's effect, I added a border of each textView content.

In "Match parent" textView content, we can see it's layout width spread out of it's parent whole length.

But we can see in "Wrap Content" textView content, it's layout width wrapped in of it's content(Wrap Content) length.

Android Layout

Subarata Talukder
  • 2,839
  • 22
  • 27
3

To me fill parent and match parent performs the same function only that:

fill parent: Was used before API 8

match parent This was used from API 8+ Function of Both Fills the parent view aside the padding

Xcode
  • 95
  • 1
  • 11
2

FILL_PARENT was renamed MATCH_PARENT in API Level 8 and higher which means that the view wants to be as big as its parent (minus padding) - Google

2

FILL_PARENT is deprecated from the API level 8 and higher and it is renamed for the upper versions as MATCH_PARENT

Both are same FILL_PARENT and MATCH_PARENT,FILL_PARENT was used in the lower version less than API level 8 and MATCH_PATENT are used in higher API level greater than 8.

FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)

fill_parent: The view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by match_parent.

For more details please visit this page

Vineeth Sai
  • 3,113
  • 7
  • 19
  • 28
0

1. match_parent

When you set layout width and height as match_parent, it will occupy the complete area that the parent view has, i.e. it will be as big as the parent.

Note : If parent is applied a padding then that space would not be included.

When we create a layout.xml by default we have RelativeLayout as default parent View with android:layout_width="match_parent" and android:layout_height="match_parent" i.e it occupies the complete width and height of the mobile screen.

Also note that padding is applied to all sides,

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

Now lets add a sub-view LinearLayout and sets its layout_width="match_parent" and layout_height="match_parent", the graphical view would display something like this,

match_parent_example

Code

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.code2care.android.togglebuttonexample.MainActivity" >

android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="11dp"
android:background="#FFFFEE"
android:orientation="vertical" >

2. fill_parent :

This is same as match_parent, fill_parent was depreciated in API level 8. So if you are using API level 8 or above you must avoid using fill_parent

Lets follow the same steps as we did for match_parent, just instead use fill_parent everywhere.

You would see that there is no difference in behaviour in both fill_parent and match parent.

hossam scott
  • 442
  • 5
  • 27
0

Both, FILL_PARENT and MATCH_PARENT are the same properties. FILL_PARENT was deprecated in API level 8.