Home > Articles > Mobile Application Development & Programming

  • Print
  • + Share This
From the author of How Do Action Bars Behave?

How Do Action Bars Behave?

The tricky part is that devices running Honeycomb and later display action bar functionality differently, depending upon whether your application's target SDK is set to Honeycomb or a legacy API level.

Let's look at a simple example. Let's assume we have an app with four screens: a main activity to launch into, and three other "cleaning" activities for sweeping, scrubbing, and vacuuming. Now, let's add an options menu to our main activity that allows the user to jump to the three "cleaning" features easily (Figure 2).

Figure 2 A simple app with an options menu and four screens (Click Image to Enlarge)

The two basic components of this app are the options menu resource file and the main activity class. The other activity classes simply display an ImageView and a TextView control. The options menu resource file simply defines the options menu items:

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android=">http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/sweep"
        android:icon="@drawable/ic_menu_sweep"
        android:title="@string/sweep" 
        android:onClick="onOptionSweep" />
    <item
        android:id="@+id/scrub"
        android:icon="@drawable/ic_menu_scrub"
        android:title="@string/scrub"
        android:onClick="onOptionScrub" />
    <item
        android:id="@+id/vacuum"
        android:icon="@drawable/ic_menu_vac"
        android:title="@string/vacuum" 
        android:onClick="onOptionVacuum" />
</menu>

The main activity class loads this menu resource as an options menu, and defines the onClick handlers for each options menu item, as follows:

package com.mamlambo.actonthis;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class ActOnThisActivity extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.cleaningoptions, menu);
      return true;
   }
      public void onOptionSweep(MenuItem i)
   {
      startActivity(new Intent(this, SweepActivity.class));
   }
   public void onOptionScrub(MenuItem i)
   {
      startActivity(new Intent(this, ScrubActivity.class));
   }
      public void onOptionVacuum(MenuItem i)
   {
      startActivity(new Intent(this, VacuumActivity.class));
   }
   }

We aren't doing anything fancy here, so we technically do not need to set the application's Android manifest file to a high target API level. So let's say we simply set it to something older, such as API Level 9:

<uses-sdk android:minSdkVersion="9" />

When we run this "legacy" application on a Honeycomb device, the system bar will show a fourth icon that looks like a grid. This is the software button equivalent of the Menu button found on traditional Android phones. Clicking it will show the options menu, much as it would on an older smartphone (Figure 3). The title bar at the top of the screen is simply a skinny bar that shows the application's title.

Figure 3 Legacy application behavior on a Honeycomb device (Click Image to Enlarge)

If we modify the target API level of the application's Android manifest file and set it to API Level 11 (Honeycomb), the action bar mechanism is automatically applied to the application.

<uses-sdk android:minSdkVersion="11" />

By default, the title bar is now thicker. It shows the application icon, the application name, and what is called the overflow menu icon. Clicking this icon results in a textual menu that lists the options menu items (Figure 4).

Figure 4 Honeycomb application behavior on a Honeycomb device (Click Image to Enlarge)

  • + Share This
  • 🔖 Save To Your Account