18

I am building an application with one Activity MainActivity which consists of two fragments. And I came across with many tutorials that use FrameLayout as the parent container for the fragment layout.

However, I came to know that I can still use LinearLayout as parent container of the fragment and it works perfectly fine (so far).

My question, is there any side effect(s) of not using FrameLayout as the parent container for the fragment layout?

If there isn't, then what is the advantage of using FrameLayout over LinearLayout (or other possible layout) as the parent container of the fragment layout.

Ankush Bist
  • 1,862
  • 1
  • 15
  • 32
fruqi
  • 4,983
  • 4
  • 26
  • 32

3 Answers3

22

Fragments themselves are rendered just like any View, so you can use whatever parent ViewGroup you would like depending on how you want your layout to look. This means there's no inherent benefit of LinearLayout vs FrameLayout tied to Fragments at all.

The main difference between FrameLayout and LinearLayout is that Views stack within a FrameLayout.

In other words, if you want your Fragments to potentially overlap, use a FrameLayout. If you want them displayed linearly, use a LinearLayout. If it's a fullscreen Fragment then it likely doesn't matter which you choose.

telkins
  • 10,440
  • 8
  • 52
  • 79
8

FrameLayout is more efficient than LinearLayout and RelativeLayout.Because it is the simplest base layout,and there are no association between child layouts. FrameLayout is suitable for stack-up views,and often used with ScrollView.

banking
  • 480
  • 2
  • 9
5

It's Perfectly fine. You can use. But, in general :

LinearLayout is used for aligning views one by one vertically or horizontally. so that multiple child views can arrange accordingly.

FrameLayout is used to load views one above another. so FrameLayout use to hold single child view because it is difficult to manage multiple view in FrameLayout. For Ex. if you want to use single CardView then FrameLayout is perfect.

Prabs
  • 4,923
  • 6
  • 38
  • 59
Pitty
  • 1,907
  • 5
  • 16
  • 34