0% found this document useful (0 votes)
26 views

Android Attributes

The document explains the differences between wrap_content and fill_parent in Android layouts. Wrap_content sizes a component only as big as its content, while fill_parent sizes it to fill its parent container. Examples are given showing a button sized using each approach for width and height.

Uploaded by

Imran S
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Android Attributes

The document explains the differences between wrap_content and fill_parent in Android layouts. Wrap_content sizes a component only as big as its content, while fill_parent sizes it to fill its parent container. Examples are given showing a button sized using each approach for width and height.

Uploaded by

Imran S
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Android wrap_content
2. fill_parent example
3. fill_parent – height
4. fill_parent – width, height
Android wrap_content and fill_parent example
• In Android, you always put either
“wrap_content” or “fill_parent” on component’s
attribute “layout_width” and “layout_height“,
did you wonder what’s the different?
• See following definition :
• wrap_content – The component just want to
display big enough to enclose its content only.
• 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)
• 1. wrap_content:
• A button component, set “wrap_content” on
both width and height attribute. It tell Android
to display the button big enough to enclose
it’s content “Button ABC” only.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/re
s/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button android:id="@+id/btnButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button ABC"/>
</RelativeLayout>
2. fill_parent – width

• Change the “layout_width” to “fill_parent“,


now, the button’s width will fill in the
remaining spaces, just as big as it’s parent
“RelativeLayout“, but button’s height is still big
enough to enclose it’s content only.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com
/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button android:id="@+id/btnButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button ABC"/>
</RelativeLayout>
3. fill_parent – height

• Change the “layout_height” to “fill_parent“,


now, the button’s height will fill in the
remaining spaces, just as big as it’s parent
“RelativeLayout“, but button’s width is still big
enough to enclose it’s content only.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btnButton1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Button ABC"/>
</RelativeLayout>
4. fill_parent – width, height
• Change the both “layout_width” and
“layout_height” to “fill_parent“, the button
will display as big as the whole device screen,
it just fill in the entire screen space.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btnButton1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Button ABC"/>
</RelativeLayout>

You might also like