-1

All examples I find for FrameLayout use the FrameLayout to stack multiple children on top of each other. Even e.g. Stackoverflow answer here says

You use a FrameLayout to stack child views on top of each other,

The official documentation however states

FrameLayout 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,

While adding multiple children to it is possible, it sounds like it's just a side effect.

So, what's the originally intended use case for a FrameLayout with a single item? Anyone has an example for that?

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
  • check this https://stackoverflow.com/questions/25679369/what-does-framelayout-do – Elias Fazel Jan 28 '20 at 22:31
  • It's the simplest layout possible, it lays out each child view based only on its size and gravity disregarding other children which creates the effect of view stacking on top. – Pawel Jan 28 '20 at 22:50
  • Elias, I cant find an example with a SINGLE child in the linked topic ): Pawel, thanks I am aware of this yet wondering what's the use case for a single child. – stefan.at.kotlin Jan 28 '20 at 23:04

1 Answers1

0

There are different purposes why FrameLayout with only one child can be useful. Imagine having a loading spinner that should be centered but not fill the whole page but also you want the content to be hidden when the loading spinner is shown. Possible solution: FrameLayout with match_parent for width and height and a background color and the ProgressBar (Loading spinner) with android:layout_gravity=center as only child of the FrameLayout. So now you can set the android:visibility of the FrameLayout to show or hide the whole thing.

antoshkaa
  • 110
  • 1
  • 5
  • hmm but how would I place the Frame Layout with the spinner on top of the content Layout, e.g. a LinearLayout? – stefan.at.kotlin Jan 28 '20 at 23:00
  • The parent component needs to be a view that supports that, so for example you could use a FrameLayout as well and then embed your real content into a LinearLayout that is direct child of the FrameLayout and uses the complete width. But you could also use a ConstraintLayout. Many possibilities here... – antoshkaa Jan 30 '20 at 09:19
  • So you mean FrameLayoutA -> LinearLayout (for content) | FrameLayoutB (for spinner). In this case FrameLayoutA has to children, but I care about examples for FrameLayouts with one children. FrameLayoutB has only one children (the spinner), but I don't see the necessity to use a FrameLayoutB for the spinner - couldn't this just also be e.g. a LinearLayout? So there is still no real use case for a FrameLayout with only one children, while the documentation seems to state that this (only a single children) is the intended use case for a FrameLayout? – stefan.at.kotlin Jan 30 '20 at 18:47
  • Afaik having only one children inside of a LinearLayout to then position it centered is not the supposed functionality of LinearLayout. I don't know if it has a big effect on performance but if you consider having one child inside of a LinearLayout with full width and height to then make the child be centered you better use a FrameLayout. – antoshkaa Jan 31 '20 at 14:42