Android ActionBar Quick Tutorial

May 8th, 2013 Leave a comment
Like the article?
Android ActionBar Quick Tutorial

For those of you not in the loop, the ActionBar a bar located at the very top of an activity that can show the activity icon, title, and actions that can be triggered (hence its name): things like additional views, other interactive buttons, app navigation, and so on and so forth. This replaces the older Android OptionsMenu that was opened when a user pressed the hardware Option button; the ActionBar is preferable to it as it is clearly visible and shows the user what they can do and where they can do it. Read on for how to implement this in your app!

You use an activity to populate the ActionBar in its onCreateOptionsMenu() method. These entries are called actions, and they’re normally defined in a typical XML resource file. The MenuInflator class is used to inflate those actions set in the XML file and add them to an ActionBar.

You can use the showAsAction attribute to control how the action is displayed: for example, the ifRoom attribute labels the action as one that is only shown if there is enough room available for it.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@drawable/ic_action_search"
        android:title="settings"/>
<item
        android:id="@+id/action_kablam"
        android:title="Kablam!">
</item>

</menu>

Once you have the XML ready to go, you can use the menuInflater() method to access them:


@Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu);
    return true;
  }

Reacting to an action is as follows: once an action is selected, the onOptionsItemSelected() method gets called. It takes the selected action as a parameter, and based on this you decide what it should do.


@Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menuitem1:
      Toast.makeText(this, "Number 1 on deck!", Toast.LENGTH_SHORT)
          .show();
      break;
    case R.id.menuitem2:
      Toast.makeText(this, "Number 2 on deck!", Toast.LENGTH_SHORT)
          .show();
      break;

    default:
      break;
    }

    return true;
  }

Also, a couple of things to note at this juncture: to change menu items, you have to use the invalidateOptionsMenu() method, since onCreateOptionsMenu() is called only once. Also, you can search by id using the findItem() method of the Menu class – something handy to know!

Conclusion

And there it is – a quick and dirty introduction to using ActionBars in Android. There’s a whole lot more you can do with them, but that goes beyond the scope of this introduction – we do cover ActionBar in detail in our Android Bootcamp class. So go forth, hit the API, and give your app the much-vaunted power of the ActionBar! Also, check out our new Android tutorial section!

Help us spread the word!
  • Twitter
  • Facebook
  • LinkedIn
  • Pinterest
  • Delicious
  • DZone
  • Reddit
  • Sphinn
  • StumbleUpon
  • Google Plus
  • RSS
  • Email
  • Print
If you liked this article, consider enrolling in one of these related courses:
Don't miss another post! Receive updates via email!

Comment