Android Pro Tip: Fragment meets container

Today, 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!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s