In this article I have captured my observations w.r.t ActionBar usage in Android application(s). These observations are very specific to compatibility issues I came across while using ActionBar functionality across various Android SDK versions.

Per Android developer guide, ActionBar is a dedicated piece of real estate at the top of each screen that is generally persistent throughout the app. ActionBar provides user actions and navigation modes.

ActionBar usage is simple and it adds great value to the application view. ActionBar is introduced in Android 3.0 (Honeycomb) version . So, the apps which are built using >= 3.0 can make use of this widget with-out much effort. But, the problem is with backward compatibility i.e., we can get this same code/functional flow working with pre 3.0 versions.

So, how do we get the action bar widget on pre-Honeycomb?

Well, we will discuss few options and approach here!

Well, one of the options is to develop custom action bar i.e., replace the title bar with custom component and manage buttons, menus etc… This is a programmatic approach and not viable solution.

Other option is, ActionBarSherlock.
ActionBarSherlock is an extension of the support library designed to facilitate the use of the action bar design pattern across all versions of Android with a single API.

Let us start with a sample application developed using Android 4.x SDK, which demonstrates the usage ActionBar widget.

Step 1: Create android sample application project (eg: SampleActionBar)

 

 

Complete the project creation flow by selecting default options.

Step 2:  Edit, res/menu/main.xml with following content

<?xml version=”1.0″ encoding=”utf-8″?>
<menu xmlns:android=”http://schemas.android.com/apk/res/android” >
    <item android:id=”@+id/menuitem_search”
        android:title=”@string/menu_search”
        android:icon=”@android:drawable/ic_menu_search”
        android:showAsAction=”ifRoom”>
    </item>
    <item android:id=”@+id/menuitem_add”
        android:title=”@string/menu_add”
        android:icon=”@android:drawable/ic_menu_add”
        android:showAsAction=”ifRoom”>
    </item>
</menu>

Step 3: Edit activity file to have the following code.
 

public class MainActivity extends Activity {
        public static Context appContext;
 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                appContext = getApplicationContext();
        ActionBar actionbar = getActionBar();
        actionbar.setHomeButtonEnabled(true);
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
 
        }
 
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.activity_main, menu);
                return true;
        }
       
        public boolean onOptionsItemSelected(MenuItem item) {
                switch(item.getItemId()) {
                        case R.id.menuitem_search:
                                Toast.makeText(appContext, “search”, Toast.LENGTH_SHORT).show();
                                return true;
                        case R.id.menuitem_add:
                                Toast.makeText(appContext, “add”, Toast.LENGTH_SHORT).show();
                                return true;
                }
                return false;
        }
       
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(“tab”, getActionBar().getSelectedNavigationIndex());
    }

Step 4: Launch 4.2 AVD

 

 

Step 5: Run the application
Once the application view is launched, click on ‘Search’ menu item.


 

Similarly, now click on and ‘Add’ to verify the invocation.

 

Well, this flow works fine with Honeycomb and later versions. And the same is not compatible with pre Honeycomb versions.

Let us modify the application to make it compatible with older versions.

Step 1:

Download ‘ActionBarSherlock’ library from following link

http://actionbarsherlock.com/download.html

Setup this as Android project in eclipse and make sure project compiles with-out errors.

 

Step 2:

Go to ‘SampleActionbar’ properties and configure Sherlock as library.

 

 

After this configuration verify ‘project.properties’ for the following entry

android.library.reference.1=<path_to_actionbar_dir>/ActionBarSherlock-4.2.0/library

Remove ‘android-support-v4.jar’ from libs folder in case, if you see compilation errors after configuring the sherlock library.

Also, make sure android:minSdkVersion is set to ‘7’

android:theme=”@style/Theme.Sherlock.Light.DarkActionBar” in AndroidManifest.xml

Step 3: Modify the activity to extend SherlockActivity

     public class MainActivity extends SherlockActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        }
        @Override
        public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
                getSupportMenuInflater().inflate(R.menu.activity_main, menu);
                return super.onCreateOptionsMenu(menu);
        }      
        public boolean onOptionsItemSelected(MenuItem item) {
                switch(item.getItemId()) {
                        case R.id.menuitem_search:
                                Toast.makeText(this, “search”, Toast.LENGTH_SHORT).show();
                                return true;
                        case R.id.menuitem_add:
                                Toast.makeText(this, “add”, Toast.LENGTH_SHORT).show();
                                return true;
                }
                return super.onOptionsItemSelected(item);
        }      
}

Step 4:

Run the application on pre Honeycomb versions.

 

Here is the output from Gingerbread device. Same action bar buttons are now compatible with pre Honeycomb devices.

With minimal code changes we can achieve backward compatibility.