ActionBar pre Honeycomb
Asked Answered
H

7

12

I am writing an app for android (2.1 > 3.1) and I would like to use the familiar practice of using the app Icon in Honeycomb apps to go up to the home activity, however, when I run the activity on earlier, non Honeycomb devices where the Activity.getActionBar(); method does not exist yet, the app force closes, how can I only run this specified code if the device is running honeycomb?

@Override
protected void onStart() {
    super.onStart();
    ActionBar actionBar = this.getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
}

Thanks for any help and have a great day.

Headphone answered 11/7, 2011 at 5:38 Comment(0)
L
12

Android pre-Honeycomb doesn't have an ActionBar, so any method concerning the actionBar will just fail. You should take a look at the code from the Google IO app, which uses an ActionBar both for Honeycomb and pre-Honeycomb.

Put simply, it won't work by itself, you'll have to include your own ActionBar code.

Lemon answered 11/7, 2011 at 5:41 Comment(0)
C
67

I have written a library for Android which will automatically wrap your pre-3.0 activities with a custom implementation of the action bar design pattern. You can then call getSupportActionBar() which will provide a common interface for both the native and custom implementations, depending on which version of Android your application is running on.

The library also allows you to apply custom styles to both of these action bars through a single theme.

You can find out more information as well as screenshots of sample applications at actionbarsherlock.com.

The library is 100% open source and available at github.com/JakeWharton/ActionBarSherlock.

Compassion answered 11/7, 2011 at 12:8 Comment(3)
What if I m using not SherlockActivity or simple Activity but MapsActivity which does not support sherlock and getSupportActionBar()?Feast
Google have now provided a better solutionGranite
@JakeWharton Seems like they've copied your library.. Still sticking to yours tho, great work I'm still using it with every app I create and have created.Combustible
L
12

Android pre-Honeycomb doesn't have an ActionBar, so any method concerning the actionBar will just fail. You should take a look at the code from the Google IO app, which uses an ActionBar both for Honeycomb and pre-Honeycomb.

Put simply, it won't work by itself, you'll have to include your own ActionBar code.

Lemon answered 11/7, 2011 at 5:41 Comment(0)
B
4

From the API Guide for the Action Bar it says:

the Action Bar Compatibility sample app provides an API layer and action bar layout that allows your app to use some of the ActionBar APIs and also support older versions of Android by replacing the traditional title bar with a custom action bar layout.

You can get this by installing the Android 4.1 (API 16) samples.

Then in Eclipse:

  1. Go to File > New > Project
  2. Android > Android Sample Project
  3. Check Android 4.1
  4. Select ActionBarCompat
Bumpy answered 25/7, 2012 at 5:23 Comment(2)
The ActionBarCompat project does not have setDisplayHomeAsUpEnabledLandonlandor
@Landonlandor Yes, it does, I have it working in my project: getSupportActionBar().setDisplayHomeAsUpEnabled(true);Mirellamirelle
L
3

Since the actionbar dont exist on pre honeycomb you'll have to make do with something else. One suggestion would be to use johannilssons actionbar library which can be found on github. Direct link: https://github.com/johannilsson/android-actionbar

Limnology answered 11/7, 2011 at 5:48 Comment(1)
that doesn't work as it should.... The Example provided with the download has errors...Micelle
R
3

I like to use GreenDroids action bar (plus they include some other pretty things): http://android.cyrilmottier.com/?p=240

Returnee answered 11/7, 2011 at 5:55 Comment(0)
B
3

As of Revision 18 the Android Support library contains ActionBar support back to API Level 7. This is now the recommended way to support the ActionBar for all versions of Android from 2.1 up and is significantly easier to use than third party libraries or other hacks.

Bumpy answered 28/8, 2013 at 1:45 Comment(0)
P
2

I think the code is self-explanatory

private static int sdkVersion;
 static 
 {
    try {
      sdkVersion = Integer.parseInt(android.os.Build.VERSION.SDK);
    } catch (Exception ex) {
    }
  }

  /** Device support the froyo (Android 2.2) APIs */
  public static boolean isAndroid22() {
    return sdkVersion >= 8;
  }

  /** Device support the Gingerbread (Android 2.3) APIs */
  public static boolean isAndroid23() {
    return sdkVersion >= 9;
  }

  /** Device supports the Honeycomb (Android 3.0) APIs */
  public static boolean isAndroid30() {
    return sdkVersion >= 11;
  }
Pegeen answered 11/7, 2011 at 5:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.