Android Frame Layout


Frame Layout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.

You can, however, 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.

Frame Layout

Frame Layout

FrameLayout Attributes

Following are the important attributes specific to FrameLayout −

Attribute Description
android:id This is the ID which uniquely identifies the layout.
android:foreground This defines the drawable to draw over the content and possible values may be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
android:foregroundGravity Defines the gravity to apply to the foreground drawable. The gravity defaults to fill. Possible values are top, bottom, left, right, center, center_vertical, center_horizontal etc.
android:measureAllChildren Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. Defaults to false.


This example will take you through simple steps to show how to create your own Android application using frame layout. Follow the following steps to modify the Android application we created in Hello World Example chapter −

Step Description
1 You will use Android studio IDE to create an Android application and name it as demo under a package com.example.demo as explained in the Hello World Example chapter.
2 Modify the default content of res/layout/activity_main.xml file to include few widgets in frame layout.
3 No need to change string.xml, android takes care default constants
4 Run the application to launch Android emulator and verify the result of the changes done in the application.

Following is the content of the modified main activity file src/com.example.demo/ This file can include each of the fundamental lifecycle methods.

package com.example.demo;

import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {
   protected void onCreate(Bundle savedInstanceState) {
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(, menu);
      return true;

Following will be the content of res/layout/activity_main.xml file −

<FrameLayout xmlns:android=""
      android:text="Frame Demo"

Following will be the content of res/values/strings.xml to define two new constants −

<?xml version="1.0" encoding="utf-8"?>
   <string name="app_name">demo</string>
   <string name="action_settings">Settings</string>

Let's try to run our modified Hello World! application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Android Studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Android Studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window −

Android Frame Layout