Android fill_parent and match_parent with Example

A new Android developer may get confused about what’s the difference between fill_parent,match_parent and wrap_content.
In android every Tag in Xml file contains two important attributes
  • android:layout_width
  • android:layout_height
Did you wonder what’s the different?

Android fill_parent and match_parent

Lets see the Definition,
  1. wrap_content – The component just want to display big enough to enclose its content only.
For Example : if you are declaring button name as “Submit” and set its heigh & width as wrap_content then it will creates button contain will ” button ” name size and dose not occupy more then its content area.
  1. fill_parent -The component want to display as big as its parent, and fill in the remaining spaces.
For example :  if your parent tag has 120 dp set into width and also 120 dp into height then it will cover your whole parent area. Here parent are called as your main above first defining layout tag.
Must see  : Android Menu Tutorials
Lets See an Examples for better understanding
Activity.xml
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”
android:background=”#33FF00FF”>
<!– This button is used to show use of wrap_content –>
<Button
android:id=”@+id/button1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Wrap-content” />
<!– As layout_width=fill_parent the button will fill the screen horizontally –>
<Button
android:id=”@+id/button2″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Fill-parent” />
<!– This is an inner layout which is parent of Button-3 –>
<LinearLayout
android:layout_width=”200dp”
android:layout_height=”match_parent”
android:orientation=”vertical”
android:background=”#7701b1d6″>
<!– Layout width is made fill_parent, so this button will fill the parent i.e. the Blue area.   –>
<Button
android:id=”@+id/button3″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Fill-parent” />
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.wrapcontentfillparent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Output

Conclusion :
fill_parent and match_parent are the same, used when we want the height or width of a view to be as big as its parent view, fill_parent being deprecated.
wrap_content is used when we want the view to occupy only as much space as required by it.
You may also read : Android UI Layouts

Comments

Popular posts from this blog

Google Declared Best Android Apps Of 2017: No Place For Facebook Or Whatsapp That Make Everyone Love It

Steps for Android Studio - Environment Setup

Core Building Blocks of Android