Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 944
Ankit Avatar asked May 21 '12 06:05

Ankit


People also ask

Why do we prefer FrameLayout for fragments?

Main purpose of frame layout is to block the area required to fit the largest child view. If you use a Frame Layout as Fragment Container you can ensure that you always have the space available to accommodate the largest fragment layout.

How does FrameLayout work on Android?

Android Framelayout is a ViewGroup subclass that is used to specify the position of multiple views placed on top of each other to represent a single view screen. Generally, we can say FrameLayout simply blocks a particular area on the screen to display a single view.

What is the difference between FrameLayout and RelativeLayout in Android?

RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets. TableLayout : is a view that groups its child views into rows and columns. FrameLayout : is a placeholder on screen that is used to display a single ...

What is the difference between constraint layout and linear layout?

In Linear Layout the UI which is actually seen in the Design editor of Android Studio will be the same as that we will get to see in the app, but in the case of Constraint layout if the UI component is not Constrained then the UI will not look the same as that of in design editor.


1 Answers

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.

like image 141
alkas Avatar answered Sep 27 '22 23:09

alkas