Android FrameLayout


Download Android FrameLayout Sample

FrameLayout is a simple layout to design user interface in Android applications. Frame layout can be used to design user interface if we have at least one of these 2 requirement...

  • We want to display a single view at a time
  • We want to overlap 2 or more views on the screen. FrameLayout automatically stacks its children on top of each other. This makes it really easy to implement overlay or HUD element in your interface.

In Frame layout, child views are drawn in a stack, with the most recently added child on top. The size of the FrameLayout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits). Views that are GONE are used for sizing only if setConsiderGoneChildrenWhenMeasuring() is set to true.

We can add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.


Example

Let's move on to our demo. In this example, an image is covering full width and height of screen and other images and text views are overlapping it. .

Android FrameLayout

Layout file of above result is given below.
activity_main.xml


Xml attributes of Android FrameLayout

Here is some useful xml attributes of android frame layout

Xml attributes Description
android:foreground android:foreground points to the foreground that your Framelayout needs to draw on top of all its children
android:foregroundGravity

android:foregroundGravity points to the gravity of that asset, if that asset is a drawable. That means if you are using

android:foreground="@android:drawable/bottom_bar"

Then your drawable is drawn on top of all its children and it is positioned in the center of the Framelayout.

android:measureAllChildren android:measureAllChildren determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. Defaults to false. That means, if it is set to false, then all the elements in the GONE state are not taken into consideration when the Framelayout's measure phase is going around.

Here is another example of using android:foregroundGravity and

android:foregroundGravity

Android FrameLayout

Layout file of above result is given below.
frame_layout.xml


Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.