Adding Drop down navigation to action bar in Android

July 23, 2012
By

In this article, we will see how to add a dropdown navigation to an actionbar in Android.

This application is developed in Eclipse 3.7.2 and tested in Android Virtual Device of API level 14.


1. Create an Android project namely “ActionBarDropdownNavigation”

New Android Project

Figure 1 : New Android Project


2. Select Android Build target

Select Android Build Target

Figure 2 : Select Android Build Target


3. Enter application details

Enter Application Details

Figure 3 : Enter Application Details


4. Open and update the file src/in/wptrafficanalyzer/actionbardropdownnavigation/MainActivity.java


package in.wptrafficanalyzer.actionbardropdownnavigation;

import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {

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

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        /** Enabling dropdown list navigation for the action bar */
        getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

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

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

        /** Setting dropdown items and item navigation listener for the actionbar */
        getActionBar().setListNavigationCallbacks(adapter, navigationListener);
    }
}


5. Application Screenshot

Application in Execution

Figure 4 : Application in Execution


6. Download


7. 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: , , , ,

12 Responses to Adding Drop down navigation to action bar in Android

  1. plancys on October 4, 2012 at 2:24 am

    There is a posibility to have the same visual effect in API 7/8 ?

  2. Andy on January 16, 2013 at 5:57 pm

    Dude awesome, keep on writing small snippets like this, from last one day i was trying to built action with custom dropdown menu but was not able to get it , you did it for me man

  3. ajay Kum on March 11, 2013 at 12:54 pm

    Thank u so much. what I wanted I got from u.

  4. Jesse on March 11, 2013 at 7:47 pm

    I just wanted to inquire how you the “Bookmark” to stay dark and the drop down menu to be grey with white text? How is this accomplished? Is there a way to set a title like “Main Menu” or “Select One” and then when you open the drop down it’s just the list without the title?

  5. Bashir Khan on July 21, 2013 at 12:51 am

    thnxxxxxxx.

  6. Aidil on July 31, 2013 at 2:59 pm

    How come after adding it, my dropdown list is dark and follows the actionbar background.. while the font is dark? How can i change it to white?

  7. reader on August 1, 2013 at 4:58 pm

    you should use getApplicationContext() or simply “this”, instead of getBaseContext, check the official Android Developer blog on this

  8. Suraj on February 3, 2014 at 3:00 pm

    i want to display custom drop down menu at left top corner,please suggest any tutorial ..

  9. Kim Arjay on July 6, 2015 at 7:06 pm

    how to add intent there in action bar..kindly help me..thank you

  10. Kim Arjay on July 6, 2015 at 7:07 pm

    how to inetent there in navigation bar then it will turn into the next XML file..kindy help me..thank you

  11. laberto on July 26, 2015 at 5:22 am

    Buen ejemplo me ayudo mucho… gracias bro…

  12. Nagini on December 1, 2015 at 3:21 pm

    im getting error at setListNavigationCallBacks…please help..

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