AndroidDev (Pro) tip: Fragment meets container

Today a tip from my colleague Coen Houtman, a tip about Fragments and the container to put them in. What all of you Fragment users know, is that you must have a container (i.e. ViewGroup) to put your fragments in.

Which ViewGroup is the best to use?

Normally, with any other layout I want to create, I start with a LinearLayout. It’s simple, easy and lightweight; something that you will experience in more complex layouts. If you use LinearLayout as the container for your Fragments, you will probably run into some issues. In the image below, the content doesn’t even take up half the screen. Even though the width was set to match it’s parent.

device-2012-12-14-111146

The solution to the problem above: use FrameLayout! I’m not claiming that FrameLayout is always the way to go. In all our projects though, we use FrameLayout as the container for our Fragments. At the moment I don’t have the answer of why FrameLayout works better than LinearLayout. But when I do, you’ll read it here!

Leave a Reply

*

captcha *