Android FrameLayout

| Page Views: 116

Android FrameLayout Tutorial and Examples

FrameLayout is a ViewGroup designed to block out an area on the screen which then you can use to display a single item.

Normally you use a FrameLayout to hold only a single view. Think of it as a placeholder.

if you use to hold multiple views, then it becomes hard to present them in a way that's scalable to different screen sizes without the children overlapping each other.

If you really have to add multiple views to a FrameLayout, then you go ahead add them but use the android:layout_gravity attribute to control their positions.

FrameLayout's child views will be painted in a stack. The one on top is the most recently added.

A FrameLayout's size resembles that of it's largets child. That child of course can have padding(in which case the padding is added also) and may be visible or not.

If you want to control size of the FrameLayout using children that are hidden by the View.GONE attribute thenyou have to make sure setConsiderGoneChildrenWhenMeasuring() is set to true.

FrameLayout API Definition

FrameLayout is a concrete class defined in the android.widget package.

It also derives from the ViewGroup class.

public class FrameLayout extends ViewGroup 
   ↳    android.view.View
       ↳    android.view.ViewGroup
           ↳    android.widget.FrameLayout

FrameLayout Subclasses

FrameLayout has very many powerful and popular subclasses. These include:

No. Name Description
1. ScrollView A viewgroup that allows the view hierarchy placed within it to be scrolled.
2. HorizontalScrollView Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display.
3. DatePicker A widget that allows us select a date
4. TimePicker A widget for selecting the time of day, in either 24-hour or AM/PM mode.

Quick FrameLayout Examples

1. How to create a Square FrameLayout

import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;

public class SquareFramelayout extends FrameLayout {
    public SquareFramelayout(Context context) {

    public SquareFramelayout(Context context, AttributeSet attrs) {
        super(context, attrs);

    public SquareFramelayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post