Tell It To The Blog

To content | To menu | To search

Tag - android

Entries feed - Comments feed

Friday 25 March 2011

Super Simple Fragments

Fragments are one of the new tablet-oriented UI features in Android 3.0 "Honeycomb". The Android developer documentation illustrates fragments with a simple but useful sample program.

Sometimes usefulness impedes understanding. Here is a useless program that will allow you to start using fragments in the simplest possible way. Follow along as I convert the Android SDK's standard "Hello World" skeleton app to use fragments.

Step 1: Hello World!

  • 1.1. Use the Eclipse IDE to create a new Android project.
  • 1.2. Choose Android 3.0, and use Min SDK Version 11.

Step 2: Layout

  • 2.1. Open res/layout/main.xml.
  • 2.2. Cut out the TextView and save it in a new XML resource: hello_frag.xml.
  • 2.3. Be sure to add the Android XML namespace in the TextView. The resulting file should look like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
/>
  • 2.4. Now add this fragment to main.xml in the LinearLayout where the TextView used to be.
<fragment class="[your main Activity class name]$SSFFragment"
   android:id="@+id/frag"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />

Step 3: Program code

  • 3.1. Open the source code file in src/[your package name]/[your main Activity class name].java.
  • 3.2. Add this private static subclass to handle the Fragment.
/* Add a class to handle fragment */
public static class SSFFragment extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View v = inflater.inflate(R.layout.hello_frag, container, false);
      return v;
   }
}
  • 3.3. In the Eclipse menu bar, select Source > Organize Imports.

Step 4: Run!

Congratulations, you are using Fragments.

Step 5: Extra credit - make it backward-compatible

  • 5.1. If you haven't already, in the Eclipse IDE, select Window > Android SDK and AVD Manager and install the Android Compatibility package.
  • 5.2. In the Eclipse menu bar, select Project > Properties > Java Build Path > Libraries.
  • 5.3. Select Add External Jars... and choose android-support-v4.jar from your file system.
  • 5.4. In your source code file, change "Activity" to "FragmentActivity".
  • 5.5. Select Source > Organize Imports again.
  • 5.6. Build and run.

Note that adding the compatibility library will have consequences:

  • It will bulk up your application. In the Hello World example, classes.dex grows from 4k to 119k.
  • Your application will not benefit from improvements to Fragments in later updates of Android.

Step 6: Extra extra credit - update deprecated layout

  • 6.1. In the TextView, replace "fill-parent" with "match-parent".

That's it! Super simple, na?

Racing ahead with Android

Racer Android

Today, the blog begins moving in a new direction: Androidward. From time to time, if I've had enough coffee, a tidbit about Android app development may appear here. If I haven't had any coffee, or if I'm just feeling ornery, I'll probably write about ferrets instead.