Android wrap_content and fill_parent example

In android every Tag in Xml file contains two important attributes
  1.  android:layout_width
  2.  android:layout_height
This Attribute take value either wrap_content or fill_parent and this value specify the height or width of the requested View.

Lets see the Definition

1.     wrap_content: – The component just want to display big enough to enclose its content only.

2.     fill_parent :– The component want to display as big as its parent, and fill in the remaining spaces. (renamed match_parent in API Level 8) .


Lets See an Examples for better understanding

Create an android xml file that contains Button inside linear layout and change the attribute values of android:layout_width and android:layout_height for button view and see the changes.


1. View With
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pavan"
        android:textSize="30dp" />

</LinearLayout>




2. View With
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Pavan"
        android:textSize="30dp" />

</LinearLayout>



3. View With
             android:layout_width="wrap_content"
             android:layout_height="fill_parent"


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Pavan"
        android:textSize="30dp" />

</LinearLayout>



4. View With
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Pavan"
        android:textSize="30dp" />

</LinearLayout>


3 comments:

Arnaud FARINE said...

Hey guy !
fill_parent should become deprecated for 4 versions, you must use Match_parent replacing fill_parent

MASK said...

@Arnaud FARINE You are right. This was my first thought as well, when I looked into the tut. But good job Pavan

kumaravel karunakaran said...

I want to know exact diffrent b/w fill_parent and ,match_parent.

Post a Comment