Tuesday, February 28, 2012


In my first technical blog item I want to talk about Android Fragments. What are Android Fragments and what are they used for? And how can it help me building my Android application? This will all be discussed in this article.

First you have to know what an activity is. An activity is a single UI screen in a Android application. Every Android application consist of one or multiple activity's. Switching between screens happens by switching between activity's. Visible items on the activity's are called view's. 

What is a Fragment?

In Android 3.0 (and above) you have the ability to use fragments. So, what are those fragments? A Fragment is a partial of the screen (or fullscreen) that has it own life cycle. It recieves it own input events and can be reused on multiple activity's. You can also use multiple Fragments inside one activity.

An example how you use fragments inside in an activity can be seen in the picture below:

In the example above you see two fragments. The green fragment is a list and the blue fragment shows the details of a list item. You see how can use the Fragment to differ the UI on tablets and phones.

How to create a Fragment?

You can create a fragment by creating a new class and extending the Fragment class. In this new class you have to override the onCreateView function. This function is part of the Fragment life cycle and is called to create a View. When you add the Fragment to the activity this View will be displayed. The View can be a normal view (like a ImageView, TextView, etc.), but it can also be a ViewGroup for the more complex layouts. You already have a LayoutInflater, so inflating from xml is easy. 

package com.example;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment extends Fragment {
  public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                           Bundle savedInstanceState) {
    return inflater.inflate(R.layout.mylayout, null);
In this example you see that the layout called mylayout is inflated and returned as view.

Include a Fragment in your Activity

You can include an Fragment into an Activity from xml layout (with some limitations) or from Java code.

To use your Fragment from a xml layout create a layout like the example below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    <fragment android:name="com.example.MyFragment"
            android:layout_width="match_parent"            android:layout_height="match_parent" />

The name parameter referred to the Fragment that you have created. If you show this layout in your application and view this in the Hierarchy Viewer (part of DDMS in Eclipse), you will see that there is no trace of the Fragment itself. The Fragment part is replaced by the view that the onCreateView function is returning.

Another way is to add the Fragment from code with the FragmentManager. To do this you need a layout with a ViewGroup in it. A LinearLayout for example. This will be the container ViewGroup where the fragment will be placed in. Don't forget to give it a id.

public View addFragment(Fragment aFragment) { FragmentManager fragmentManager = getFragmentManager() FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.fragment_container, aFragment); fragmentTransaction.commit(); }
In this example you see a function that will add the given Fragment to the layout. It will be played inside the ViewGroup with the id fragment_container. The Fragment manager is needed to get a FragmentTransaction object. In this FragmentTransaction object you can do one or multiple changes to the Fragments on the Activity. 

This is it. If you have any questions you can ask them below and I will try to answer that as good as possible. Good luck coding your Fragments.

No comments:

Post a Comment