FrameLayout vs RelativeLayout for overlays
Asked Answered
O

1

61

I need to implement an overlay (translucent) screen for my app, something similar to Showcase View

My guess was to use FrameLayout for this usecase, because it is used to stack items on top of each other. But I was surprised to see that the above library uses RelativeLayout.

My question is when to use FrameLayout then, if not in cases like this? What are the disadvantages if I go the FrameLayout way?

Oversold answered 5/4, 2014 at 1:16 Comment(0)
T
65

A common rule of thumb when choosing layouts is to select the combination that results in the smallest number of nested layout views.

Specific to your question, RelativeLayout is larger and more capable than the much simpler FrameLayout. So for simple layouts, the latter is probably more efficient. But if using RelativeLayout and it's added positioning options allows you to implement your GUI in a smaller number of layout views, then that would likely be a better choice.

Here's a page that discusses some trade-offs and demonstrates some helpful tools to use when designing your layouts. It mostly talks about RelativeLayout and LinearLayout, but is also apropos to your choice between RelativeLayout and Framelayout. Just keep in mind that FrameLayout is an even simpler layout.

Edit (2017): For even more complicated layouts, you may be able to avoid nested layouts by using ConstraintLayout.

Titania answered 5/4, 2014 at 1:42 Comment(4)
I replaced the broken link with hopefully a longer-lived one from the Android Developers Reference. The new link isn't as on-point as the previous one, but it does show how to use a couple of the layout performance tools.Titania
Hello, is FrameLayout the best layout for example a signup page layout? For example, you can't fit all the details inside one layout page so you have to include multiple pages to collect all the details of the user such as name, address, phone number, and etc. Well, I could also use a scroll view but a framelayout with multiple frames would look better. I'm asking about layout optimization. Thanks.Spheroid
FrameLayout can be useful as a simple container that will have different fragments displayed in it. But these fragments, which will each have their own layouts, would most likely use one of the others since FrameLayout doesn't allow for positioning of it's included views.Titania
What I do is just change visibility of the framelayouts everytime I press NEXT or BACK button. page1.visibility = ConstraintLayout.GONE page2.visibility = ConstraintLayout.VISIBLE (and vice versa) I have not explored Fragments yet. I was wondering if Fragments is a better alternative vs. different Activities + FrameLayouts.Spheroid

© 2022 - 2024 — McMap. All rights reserved.