What's the Difference Between Fill_Parent and Wrap_Content

What's the difference between fill_parent and wrap_content?

Either attribute can be applied to View's (visual control) horizontal or vertical size. It's used to set a View or Layouts size based on either it's contents or the size of it's parent layout rather than explicitly specifying a dimension.

fill_parent (deprecated and renamed MATCH_PARENT in API Level 8 and higher)

Setting the layout of a widget to fill_parent will force it to expand to take up as much space as is available within the layout element it's been placed in. It's roughly equivalent of setting the dockstyle of a Windows Form Control to Fill.

Setting a top level layout or control to fill_parent will force it to take up the whole screen.

wrap_content

Setting a View's size to wrap_content will force it to expand only far enough to contain the values (or child controls) it contains. For controls -- like text boxes (TextView) or images (ImageView) -- this will wrap the text or image being shown. For layout elements it will resize the layout to fit the controls / layouts added as its children.

It's roughly the equivalent of setting a Windows Form Control's Autosize property to True.

Online Documentation

There's some details in the Android code documentation here.

What is the difference between match_parent and fill_parent?

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

Android wrap_content and fill_parent at the same time?

  1. No, there is no way to set a view's width/height to both wrap_content and fill_parent simultaneously.

  2. What exactly are you trying to achieve? Is this what you mean?

    <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    ...
    android:background="@drawable/image" >

    <TextView
    android:layout_width="wrap_content"
    android:layout_width="wrap_content"
    ... >
    </TextView>

    </LinearLayout>

EDIT

If you want to nicely scale a background image for a TextView, I think what you might want to look into is a 9-patch image. See this post for more info. Also this.

fill_parent vs wrap_content listview

Try this out: I have made necessary modifications

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView" android:layout_alignBottom="@+id/button"
android:layout_alignParentTop="true"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/textView"/>
</LinearLayout>

<ListView
android:id="@+id/s1ListView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2" />
</LinearLayout>

combining wrap_content on parent and fill_parent on child

In theory what you are describing should not work ("Because it the parent gets it's height from the childs and vice-versa".) However, we made it work in LinearLayout because it was a very common use case. I recently added similar support to FrameLayout (this feature should be part of Honeycomb.) What you are doing is therefore perfectly valid and will work just fine.



Related Topics



Leave a reply



Submit