What is the purpose of FrameLayout and in which scenario should we use it?
Asked Answered
Y

2

8

What purpose does FrameLayout serve in Android? Is there any specific scenario for which it is designed for?

In my Android application, I have a scenario where I have to show two ImageViews, one over the other. This is a .png image file with a 9-patch drawable over this image.

Which ViewGroup should I use for this purpose: RelativeLayout or FrameLayout?

I also want to know the different scenarios that each of the ViewGroups should be used for.

Yuri answered 21/5, 2012 at 6:28 Comment(0)
Z
-5

Yes, you can use a FrameLayout in your scenario.

Zarger answered 21/5, 2012 at 6:43 Comment(2)
I can use or I should use ? :) Are there any performance issues related to using Frame or Relative layout? Hope u are getting my point.Yuri
@Yuri I am not sure about performance issues (I would assume FrameLayout has a smaller memory footprint, because its simpler). FrameLayout is much easier to configure (just Gravity.LEFT, Gravity.RIGHT etc.) RelativeLayout is much more complicated and more configurable. Just choose the complexity that you need for the situation.Peculiarize
R
1

I don't recall how i got to this question but here is my answer that could help anyone:

As you can see here

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

In general is not useful to have 2 views one over the other because you're going to have overdraw effect (an area of the screen that is getting drawn by the gpu more than once which is really useless).

If you really have to, then you need to use the onDraw method (you can see an example here) and Canvas API (have a look here) and the clipRect method (you can see an example here)

Regarding FrameLayout or RelativeLayout, if you want to keep things simple or your activity has already enough nested layouts, better use FrameLayout. In 2017, there is the constraint layout which could be of some help as well.

Richerson answered 16/11, 2017 at 22:49 Comment(0)
Z
-5

Yes, you can use a FrameLayout in your scenario.

Zarger answered 21/5, 2012 at 6:43 Comment(2)
I can use or I should use ? :) Are there any performance issues related to using Frame or Relative layout? Hope u are getting my point.Yuri
@Yuri I am not sure about performance issues (I would assume FrameLayout has a smaller memory footprint, because its simpler). FrameLayout is much easier to configure (just Gravity.LEFT, Gravity.RIGHT etc.) RelativeLayout is much more complicated and more configurable. Just choose the complexity that you need for the situation.Peculiarize

© 2022 - 2024 — McMap. All rights reserved.