Create Flexible Android UIs with Fragments
First introduced in Android 3.0 (Honeycomb), the Fragment API is now available on all current Android platform versions via a compatibility package provided by Google. This library makes using the new Fragment API an effective way to develop flexible user interfaces for Android. Read on to learn how to use Fragments in your applications, including those written to API Level 4 (Android 1.6) and up.
What Is an Android Fragment?
Before we begin, let's talk a bit about what fragments are. Obviously, they are a new type of Android Java object with the Fragment API. But what are they for?
Traditionally, Android applications have been made up of two basic components: a layout (the screen contents) and an associated Activity class (that comprises the functions and handles any data processing and lifecycle events for that screen). The Android fragment is a new concept that basically decouples, or loosens, the relationship between the layout visual details and the activity.
To us, a Fragment can be thought of much like a sub-activity. Fragments can do most of what an activity can do, but exist within an Activity instance. Much like an activity usually represents an entire screen, a Fragment represents a portion of a screen. Fragments can be referenced from within layouts to specify how to control different portions of a screen. When used with alternative layout techniques, fragments provide tremendous flexibility in screen design. The big advantage of a fragment (over, say, a FrameLayout object and using statements within a layout) is that not only does a Fragment handle its own lifecycle, it can exist within different activities, depending on the needs of the application.
Let's explore the ramifications of these statements. The classic Fragment example involves building an application with a screen with a top-level list and another accompanying details screen. Traditionally, these entities would be displayed on two separate screens with two different Activity classes--one screen with the ListView and another which displays the details for a chosen ListView item. Using fragments, you can make a ListView fragment and a details view fragment. Then, through simple layout changes, you can mix and match these fragments in different ways, depending on your needs and the device configuration. For example, on tablets with sufficiently large or wide screens, you might show the fragments side-by-side on the same screen, whereas on smaller screens of smartphones, you might show them on separate screens.
The sample code that accompanies this tutorial is available online in open source form. The code listings you find here are part of this sample project.
Adding the Compatibility Library to Your Android Project in Eclipse
We feel fragments are so useful and flexible that nearly every Android project can benefit from using them. Google seems to agree, since they've made the Android Compatibility package available through the Android SDK and AVD Manager, making the compatible Fragments API accessible to nearly all versions of Android.
The first thing you'll need to do is download the Android Compatibility package. After you've downloaded it, find the library by browsing to your Android SDK directory, and then finding the
/extras/android/compatibility/v4 directory. Within this directory, you'll find a file called android-support-v4.jar. Copy this file to your Android project's
/lib directory, usually created at the root of the project. (You may need to create the directory yourself if this is your first library.)
Once you've done this, edit the Eclipse project settings of your project to reference this library: Under Project, Properties, Java Build Path, choose the Libraries tab, then press the Add JARs… button. Find the /lib directory and select the android-support-v4.jar file, shown below. Finally press OK to confirm.
Once you've registered the Android Compatibility package for use within your project, you can start to use fragments. For instance, to create a class extending FragmentActivity, you'd need to import the android.support.v4.app.FragmentActivity class. When you're building against a version of Android prior to 3.0, Eclipse's automatic import organization will now automatically find this class and reference it. If you're targeting Android 3.0 and above, you may need to explicitly use this import to make sure you're building using the specific FragmentActivity version included with the compatibility library.
Layouts with Android Fragments
A fragment can be included within a layout much like other layout objects. You use the tag, along with your typical properties for width, height, and a unique identifier to define a fragment within a layout resource file. The primary difference between a View control definition and a fragment definition is that the fragment's name property must reference the fragment class name that will be responsible for drawing in that area of the screen. For example, here is a layout containing two fragments:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <fragment android:name="com.mamlambo.tutorial.fragments.SampleListFragment" android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/image_list_fragment" android:layout_weight="25"> </fragment> <fragment android:name="com.mamlambo.tutorial.fragments.SampleViewerFragment" android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/image_viewer_fragment" android:layout_weight="75"> </fragment> </LinearLayout>
Originally published on http://www.developer.com.
Page 1 of 2