Adding Drop down navigation to action bar in pre Honeycomb versions using Sherlock library

August 1, 2012
By

In the article titled “Adding Drop down navigation to action bar in Android“, we have seen how to add dropdown navigation to action bar for Android 3.0 ( Honeycomb ) and above.

In this article we will see how to add dropdown navigation to action bar for Android 2.x and above. Since action bar is available only with Android 3.0 and above, we will use Actionbar Sherlock library to create action bar in Android 2.x versions.

This application is developed in Eclipse (3.7.2 ) with ADT plugin ( 20.0.2 ) and Android SDK ( R20.0.1 )


1. Setup Actionbar Sherlock library in Eclipse IDE

Please see the article titled “Setting up Sherlock library for action bar in pre Honeycomb Android Applications


2. Create a new Android application project namely “ActionBarDropdownNavigationSherlock”

Create new Android Project

Figure 1 : Create new Android Project


3. Design application launcher icon

Design Application Launcher Icon

Figure 2 : Design Application Launcher Icon


4. Create a blank activity

Create a blank activity

Figure 3 : Create a blank activity


5. Enter MainActivity details

Enter main activity details

Figure 4: Enter main activity details


6. Delete Android’s support library

By default, Eclipse adds Android’s Support library to the project. We don’t want to use this library for this project. This library will be available in the folder “libs” in the name “android-support-v4.jar“. Delete this jar file from the “libs” folder.


7. Add Sherlock library to this project

  • Open Project Explorer
  • Right click the project   “ActionBarDropdownNavigationSherlock”
  • Open Properties
  • Select “Android” tab from the Properties Window
  • Then the given below screen will be appeared
  • Using Add button, add the Sherlock library to this project
Add Sherlock Library to this project

Figure 5 : Add Sherlock Library to this project


8. Update the class file src/in/wptrafficanalyzer/actionbardropdownnavigationsherlock/MainActivity.java


package in.wptrafficanalyzer.actionbardropdownnavigationsherlock;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockActivity;

public class MainActivity extends  SherlockActivity {

    /** An array of strings to populate dropdown list */
    String[] actions = new String[] {
        "Bookmark",
        "Subscribe",
        "Share"
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /** Create an array adapter to populate dropdownlist */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), R.layout.sherlock_spinner_item, actions);

        /** Enabling dropdown list navigation for the action bar */
        getSupportActionBar().setNavigationMode(com.actionbarsherlock.app.ActionBar.NAVIGATION_MODE_LIST);

        /** Defining Navigation listener */
        ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {

            @Override
            public boolean onNavigationItemSelected(int itemPosition, long itemId) {
                Toast.makeText(getBaseContext(), "Current Action : " + actions[itemPosition]  , Toast.LENGTH_SHORT).show();
                return false;
            }
        };

        /** Setting dropdown items and item navigation listener for the actionbar */
        getSupportActionBar().setListNavigationCallbacks(adapter, navigationListener);
        adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

    }

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        getSupportMenuInflater().inflate(R.menu.activity_main, menu);
        return super.onCreateOptionsMenu(menu);
    }
}


9. Update the file AndroidManifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.wptrafficanalyzer.actionbardropdownnavigationsherlock"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >

            <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main"
                android:theme="@style/Theme.Sherlock"
            >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
</application>

</manifest>


10. Screenshot of the application in execution

Screenshot of the application in execution

Figure 6 : Screenshot of the application in execution


11. Download


12. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , , , , ,

One Response to Adding Drop down navigation to action bar in pre Honeycomb versions using Sherlock library

  1. Android on September 17, 2012 at 4:44 pm

    A good one but the problem is that,it duplicates the “Bookmarks” menu option.from the snapshot we have 2 bookmarks, a menu and a menu option,what if i want the menu header to be bookmarks only without it being repeated in the options how do i go about it?

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner