Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBar pre Honeycomb

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.

like image 704
rabbitt Avatar asked Jul 11 '11 05:07

rabbitt


People also ask

What is the ActionBar in Android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.

What is an ActionBar?

An on-screen toolbar displaying icons that are clicked or tapped to perform various functions. For example, the menu bar at the top of an Android app is called an action bar.

What does the onOptionsItemSelected () method receives as a parameter?

If an action is selected, the onOptionsItemSelected() method in the corresponding activity is called. It receives the selected action as parameter.

What is the difference between action bar and toolbar in Android?

An Action bar is traditionally a part of an Activity opaque window decor controlled by the framework but a Toolbar may be placed at any level of nesting within a view hierarchy. The toolbar provides more feature than ActionBar . A Toolbar may contain a combination of elements from start to end.


2 Answers

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.

like image 142
Jake Wharton Avatar answered Sep 17 '22 15:09

Jake Wharton


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.

like image 20
Gregory Avatar answered Sep 17 '22 15:09

Gregory