How to implement a drop down navigation action bar in Android after you've added it?

11,363

Solution 1

You need to maniputate your ArrayAdapter if you want to change the elements, but I don't think you you can use the ArrayAdapter<String> class for that porouse. You may need to use something else than string.

For handling clicks you need to change the onNavigationItemSelected function:

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    switch(itemPosition) {
    case 0:
        Intent i = new Intent(this, SecondActivity.class);
        startActivity(i);
        break;
    case 1:
        // ...
        break;
    }
    return false;
}

Solution 2

You have to start a new Activity by calling startActivity(Intent) in the onNavigationItemSelected callback.

Solution 3

Don't know if this is the exact problem you had (though it sounds like it!), but beware using startActivity from a spinner: it can be called during onCreate().

onNavigationItemSelected in ActionBar is being called at startup how can avoid it?

Share:
11,363

Related videos on Youtube

Andrew Tsay
Author by

Andrew Tsay

Updated on September 16, 2022

Comments

  • Andrew Tsay
    Andrew Tsay over 1 year

    By following this guide, http://wptrafficanalyzer.in/blog/adding-drop-down-navigation-to-action-bar-in-android/

    I was able to add my drop down navigation bar. The click events and everything function. Now, how do I make it so once an option is selected, it navigates to a different screen with its own layout and different functions.

    Any help would be great, thanks in advance!

    Edit: This is what I have. My app runs for about a millisecond, and I can see a glimpse "Hello World" and then it crashes. I'm using Sherlock by the way, if that matters.

    package com.poe.statcalc;
    
    import com.actionbarsherlock.app.SherlockActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.Toast;
    import com.actionbarsherlock.app.ActionBar;
    import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
    
    public class MainActivity extends SherlockActivity {
    
     /** An array of strings to populate dropdown list */
    String[] actions = new String[] {
        "Bookmark",
        "Subscribe",
        "Share",
        "Something"
    
    };
    
    @Override
    protected 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) {
                switch(itemPosition) {
                case 0:
                    Intent i = new Intent(MainActivity.this, SecondActivity.class);
                    startActivity(i);
                    break;
                case 1:
                    //...
                    break;
                }
                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);
    }
    

    }

  • Andrew Tsay
    Andrew Tsay over 11 years
    I gave the code a shot, but it's crashing. Do you think you could check what's wrong?
  • rekire
    rekire over 11 years
    Did you notice this: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative. See also the ActionBarSherlock homepage how to set this.