4

Possible Duplicate:
What is the equivalent of ActionBar in earlier sdk versions?

If I'm not mistaken, the Android guidelines say you should use the ActionBar for the global navigation within an app.

But at the same time, you typically want to target the oldest API possible for maximum compatibility.

I'm beginning development on an app and I set the target to Android 2.2.

Is it possible to use the action bar here? If not, what do i use?

Thanks Kevin

Community
  • 1
  • 1
Kevin
  • 1,940
  • 3
  • 24
  • 38

3 Answers3

10

Use ActionBarSherlock.

  • Make sure your project uses Android 4.0+ as the build target and that you have the compatibility/support library added. (You can right-click the project, select Android Toots > Add Support Library)
  • Download the ActionBarSherlock library project and import it into Eclipse.
  • Right-click your main project, select Properties, and select Android in the left pane. In the bottom half of the window, click the Add button and select the ActionBarSherlock project. Click OK.
  • Instead of using regular Activities and Fragments, you use ones from the Sherlock Library. They have the same names, but prefixed with "Sherlock" (SherlockActivity, SherlockFragment, etc.)
  • To access the actionbar in code, you call getSupportActionBar(). This returns either the regular actionbar (for Honeycomb and later) or the Sherlock actionbar (for pre-Honeycomb).
  • Theming instructions are on the ActionBarSherlock website.
Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • I'm confused. If I have to target Android 4, will it not work on Android 2.2? – Kevin Jun 03 '12 at 03:43
  • I'm kind of new but I'm looking into the support library. I assume that bridges the gap? – Kevin Jun 03 '12 at 03:46
  • 1
    The support library gives you some things, most notably Fragments, but it does not contain the ActionBar. Your build target determines what version of the platform your code compiles against; you set it under Properties > Android. The actual minimum sdk you support is determined by what you put in your application's manifest file (as `android:minSdk`). You can use the latest version as your build target while declaring that your app runs on versions back to 2.2. – Karakuri Jun 03 '12 at 04:02
  • FYI: starting from v18 support library includes own ActionBar implementation – mente Aug 08 '13 at 07:49
3

There's a nice sample project called ActionBarCompat that shows how to do this. It's not 100% of what you can do when targeting API 11+, but it's pretty good.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

Definitely use ActionBarSherlock over ActionBarCompact. It's much easier to work with and is well maintained.

Community
  • 1
  • 1
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250