ActionBarCompat - how to use it

joaogoncalv_es picture joaogoncalv_es · Apr 14, 2012 · Viewed 21.9k times · Source

I'm trying to use ActionBarCompat on my own project. I've already opened the sample project (http://developer.android.com/resources/samples/ActionBarCompat/index.html), But I don't know how to implement it on my own.

I can't find any tutorial of some kind. Should I make this project as a library? Can someone give me some directions, please.

Answer

Andrés Pachon picture Andrés Pachon · Apr 25, 2012

As for implementation, just stick to the sample code provided under the MainActivity.java class. You can find it here or under <your local android-sdks folder>/samples/android-15/ActionBarCompat/src/com/example/android/actionbarcompat/MainActivity.java. In general, all you need to do is the following:

  1. Code a menu resource where you declare the items for the action bar (See http://developer.android.com/resources/samples/ActionBarCompat/res/menu/main.html)
  2. Code an Activity that extends ActionBarActivity
  3. Override onCreateOptionsMenu() so that it inflates the menu you coded in step #1
  4. Override onOptionsItemSelected() so that you handle the event when the user taps any of the ActionBar items you defined in step #1.

I think it makes sense to build an Android Library project out of the ActionBarCompat code; then you can just reference it from your custom Android project. Remember it's licensed under the Apache License, Version 2.0.