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?
16 Answers
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 bymatch_parent
.
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
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.
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!
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.
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
.
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>
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}.
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.
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.
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
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.