What does FrameLayout do?

70,457

Solution 1

You use a FrameLayout to stack child views on top of each other, with the most recent child on top of the stack. In the example below, the TextView is the most recent, so it is automatically placed on top of the ImageView.

For example:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/backgroundImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:src="@drawable/bitmapie" />

    <TextView
        android:id="@+id/descTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginTop="70dp"
        android:background="@android:color/holo_blue_light"
        android:padding="10dp"
        android:text="TextView placed at the top of the Imageview"
        android:textColor="@android:color/white"
        android:textSize="22sp" />

</FrameLayout>

Output:

enter image description here

Solution 2

FrameLayout is the simplest implementation of ViewGroup. Child views are drawn are in a stack, where the latest added view is drawn at the top. Usually you can use one of the next approaches or combine them:

  1. Add a single view hierarchy into FrameLayout
  2. Add multiple children and use android:layout_gravity to navigate them

enter image description here

Another popular approaches of using FrameLayout:

  • as a Fragment container
  • as an ancestor of your custom ViewGroup

Solution 3

You can consider the word frame as regular photo frame. What you do with that frame? you can place photos in that frame by one top to another. Same as in FrameLayout we can place views ( Any layout, or widget like button, text, image so on) top of other as @ojonugwa shows you the textview top of the Image.

Solution 4

Are you sure that you googled it?

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.

The secret of FrameLayout is how it layouts its children. Although normally designed to contain one item, it will happily stack up other element on top of each other. Thus FrameLayout is essentially a way to manipulate the Z-order of views on the screen.

This is super useful for a couple of UI tricks from HUD-like elements to sliding panels to more complex animated transitions. In this post we will see an example for each of those.

FrameLayout is designed to display a single item at a time. You can have multiple elements within a FrameLayout but each element will be positioned based on the top left of the screen. Elements that overlap will be displayed overlapping. I have created a simple XML layout using FrameLayout that shows how this works.

Solution 5

Basically it puts one view on top of another for example :

Inflating text on Image

  <FrameLayout>

<ImageView>
<Textview>

  </FrameLayout>
Share:
70,457

Related videos on Youtube

Amin Ghasemi
Author by

Amin Ghasemi

Updated on July 08, 2022

Comments

  • Amin Ghasemi
    Amin Ghasemi almost 2 years

    I'm new to programming. I was using Graphical Layout then when I was reading xml file, I saw FrameLayout. Then I searched, but I couldn't find something useful. What is FrameLayout and what does it do?

    • Amin Ghasemi
      Amin Ghasemi over 9 years
      Yes , I had been there but I couldn't find sth that be understandable .
  • Amin Ghasemi
    Amin Ghasemi over 9 years
    yes I saw them . but I couldn't findout what are they saying .
  • Metehan
    Metehan over 9 years
    You can think it as a picture frame. Basically what you put will be alligned to the left of the screen. learn-android-easily.com/2013/05/frame-layout-in-androis.htm‌​l
  • Metehan
    Metehan over 9 years
    So you should use it to display a single item for most of the times.
  • Tarik
    Tarik over 6 years
    Nice, I was thinking of it as a iframe as I am coming from web development. Thanks for the simplest explanation.
  • Tarik
    Tarik over 6 years
    I love seeing code with their output. Thanks a lot! Upvoted. Also, I like the design you made there. Simply yet very nice!
  • Coder Absolute
    Coder Absolute about 6 years
    Very simple, powerful yet easy explanation - thanks @ojonugwaochalifu!
  • Slartibartfast
    Slartibartfast about 6 years
    Very good explanation. I too read the 'Developer's description on the official page and thought the phrase "on top" meant more towards the top of the screen. Your explanation made the 3D nature of "on top" click for me and now I get it. Thank you very much :)
  • akashPatra
    akashPatra about 6 years
    @ojonugwaochalifu: thanks for the explanation. I have a small query, the things we have done here, we can do by relative layout also. Then, why we use frame layout ?
  • Ojonugwa Jude Ochalifu
    Ojonugwa Jude Ochalifu about 6 years
    Yes, they do almost the same thing.But remember, a good design practice for views is to use as little nesting as possible for your layouts. When you use a FrameLayout, the nesting required to place views on each other is less than when you use a RelativeLayout.
  • Abdalrahman Shatou
    Abdalrahman Shatou almost 5 years
    @ojonugwaochalifu No, it's not. It's the same.

Related