You have understood the concept and usage of Linear Layout that we mentioned in previous article, which display UI elements in a single row or column, Frame Layout is one of the common types of layout used by Android developers. Much like other layout, Frame Layout also can be defined using either XML or programmatically in the Java code.
The Frame Layout allow us to display only a single UI element at a time, or multiple UI elements within Frame Layout but each element will be positioned based on the top left of the screen. You can often see this layout has been used in displaying image with caption overlapping on it. For example:
This is the example using Frame Layout to organize UI elements. As you can see from above, we have placing 3 UI elements within the Frame Layout. Since Frame Layout is only able to display single UI element at a time or multiple UI elements but each of them is overlapping on each others, so you can see that, the text “This is Frame Layout!!!” and “Learn Android Development At onlyMobilePro.com” is overlapping on the onlyMobilePro logo.
Now, let’s see how can we done this using Android’s XML vocabulary.
First of all, before we start creating our XML layout, please choose an image / icon that you wish to use as a background, and place it in /res/drawable-mdpi/ project directory.
OK, please open up your main.xml file, which located at /res/layout/ and write your XML as same as shown below:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView android:src="@drawable/onlymobilepro" android:scaleType="fitCenter" android:layout_height="fill_parent" android:layout_width="fill_parent"/> <TextView android:text="This is Frame Layout!!!" android:textSize="24px" android:textColor="#cc0000" android:layout_height="fill_parent" android:layout_width="fill_parent" android:gravity="top"/> <TextView android:text="Learn Android Development At onlyMobilePro.com" android:textSize="24px" android:textColor="#00dd00" android:layout_height="fill_parent" android:layout_width="fill_parent" android:gravity="center"/> </FrameLayout>
The “android:gravity” is an attribute of TextView that used to specify how to align the text by the view’s x- and/or y-axis when the text is smaller than the view. Here is the available value that you can apply to it:
|top||0x30||Push object to the top of its container, not changing its size.|
|bottom||0x50||Push object to the bottom of its container, not changing its size.|
|left||0x03||Push object to the left of its container, not changing its size.|
|right||0x05||Push object to the right of its container, not changing its size.|
|center_vertical||0x10||Place object in the vertical center of its container, not changing its size.|
|fill_vertical||0x70||Grow the vertical size of the object if needed so it completely fills its container.|
|center_horizontal||0x01||Place object in the horizontal center of its container, not changing its size.|
|fill_horizontal||0x07||Grow the horizontal size of the object if needed so it completely fills its container.|
|center||0x11||Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.|
|fill||0x77||Grow the horizontal and vertical size of the object if needed so it completely fills its container.|
|clip_vertical||0x80|| Additional option that can be set to have the top and/or bottom edges of
the child clipped to its container’s bounds.
The clip will be based on the vertical gravity: a top gravity will clip the bottom
edge, a bottom gravity will clip the top edge, and neither will clip both edges.
|clip_horizontal||0x08|| Additional option that can be set to have the left and/or right edges of
the child clipped to its container’s bounds.
The clip will be based on the horizontal gravity: a left gravity will clip the right
edge, a right gravity will clip the left edge, and neither will clip both edges.
|start||0x00800003||Push object to the beginning of its container, not changing its size.|
|end||0x00800005||Push object to the end of its container, not changing its size.|
The Android app’s user interface – Frame layout is one of the most common and fundamental layout types used. The Frame layout allows developers to display only a single or multiple UI elements within Frame Layout but each element will be positioned based on the top left of the screen, and elements that overlap will be displayed overlapping.
Keep your mind healthy, test your mental math and exercise your brain once a day. Fun Math is a set of brain training exercises and designed to enhance mental arithmetic. View in Google Play Store
Are you indie developer? Do you know that you can promo your app by using this channel for FREE! Just Like Us on Facebook, and then post your app info in our Facebook page's wall. We will select the best one to showcase here.
640-911 , JN0-343 , CISSP , 9A0-385 , 1Z0-808 , 200-310 , LX0-103 , 74-678 , 9A0-385 , 1Z0-061 , 200-310 , 1Y0-201 , C_TFIN52_66 , 70-466 , 640-911 , E10-002 , PMP , CAS-002 , 74-678 , 1Z0-061 , 300-135 , ICGB , PMI-RMP , 300-320 , IIA-CIA-PART2 , 9A0-385 , 70-981 , 1Z0-803 , CISSP , MB5-705 , NS0-157 , 1Z0-434 , 350-080 , 1V0-603 , HP0-S41 , 9L0-012 , C_TFIN52_66 , 70-410 , 1Z0-434 , JN0-102 , EX200 , N10-006 , 9L0-066 , 220-801 , 700-260 , 1V0-601 , SSCP , 70-417 , 220-802 , 70-463 , ADM-201 , NSE4 , JN0-332 , 300-075 , 070-346 , C_TAW12_731 , 1Z0-803 , 1Z0-061 , C2010-595 , PEGACPBA71V1 , 640-692 , 210-060 , CISA , AX0-100 , 400-201 , 300-115 , ICBB , C_TAW12_731 , MB2-708 , JN0-102 , 070-466 , 1Z0-051 , PEGACSA71V1 , C_TFIN52_66 , CCA-500 , MB2-708 , 9A0-385 , 300-070 , 300-075 , 70-332 , ICGB , 599-01 , 70-494 , 2V0-620 , 1V0-603 , 700-501 , 1Y0-201 , 9L0-012 , C_TAW12_731 , 070-462 , 1Z0-144 , CBAP , 102-400 , CISSP , NSE4 , ICBB , 3002 ,