In the previous article we got tab-based navigation working. Tab-based navigation works well when you have two or three navigation options, but does not work as well when there are more. Dropdown-based navigation is better suited when we a slightly larger number number of navigation options, and in this article we’ll cover how to create dropdown-based navigation within our ActionBar.
An example of dropdown-based navigation can be found in Google Maps:
The navigation appears as a Spinner within the ActionBar, and the dropdown of the Spinner contains the navigation options. Fitting all of these options in to tab-based navigation would be extremely difficult.
To use dropdown navigation we first need to set the navigation mode of the ActionBar and remove the title text to allow more space for the spinner:
If you pass getActionBar().getThemedContext() the drop down items will display properly when using the Light.DarkActionBarTheme. In practice you should always use this method for obtaining the context for things in the action bar rather than ‘this’ for the activity context.
Another option for hiding the title is to call getActionBar().setDisplayShowTitle(false).
These articles are great. I am constantly referring new action bar users to them. Looking forward to more.
I would suggest using Jake Wharton’s ActionBarSherlock which I mentioned in Part 1 of this series. I haven’t had the opportunity to use it in a real project myself, but those that have say it’s really good, and pretty much the same as using the native Android APIs that I’m covering.
That’s done in the Adapter that you use. You can do lots of different things by using different Adapters, but they are separate from the AcrtionBar (they work with other things as well such as ListView) which is why I didn’t focus on them too much in this article. Watch this space though, I’m planning to cover Adapters in future posts.
Do you mind if I quote a few of your posts as long as I provide
credit and sources back to your blog? My blog
site is in the very same niche as yours and my users would certainly benefit from some of the information you provide here.
Please let me know if this ok with you. Thanks a lot!
The license for the articles on the site can be found here. Provided your site is non-commercial, your articles are published under the same licence terms, and you provide correct attribution then please feel free to quote my articles.
Thank you sooo much for this tutorials !… Hope that on July I buy a new computer and start programming in Android… Meanwhile I am collecting jewells like your site !… Cheers from Córdoba, Argentina (a province from the center of the country)…
If you pass getActionBar().getThemedContext() the drop down items will display properly when using the Light.DarkActionBarTheme. In practice you should always use this method for obtaining the context for things in the action bar rather than ‘this’ for the activity context.
Another option for hiding the title is to call getActionBar().setDisplayShowTitle(false).
These articles are great. I am constantly referring new action bar users to them. Looking forward to more.
Thanks Jake. I’ll update the article when I get a chance to include your suggestions.
Nice post, Mark!
I have a question: How to make action bar and this spinner work on Android 2.1? I mean, only I need to do is use the compatibility package?
Thank you for the post and sorry for my bad english! Your blog is amazing! 😀
I would suggest using Jake Wharton’s ActionBarSherlock which I mentioned in Part 1 of this series. I haven’t had the opportunity to use it in a real project myself, but those that have say it’s really good, and pretty much the same as using the native Android APIs that I’m covering.
Hi,
can you show this example with an ArrayAdapter? I am having a hard time getting to trigger.
How to set image in List?
That’s done in the Adapter that you use. You can do lots of different things by using different Adapters, but they are separate from the AcrtionBar (they work with other things as well such as ListView) which is why I didn’t focus on them too much in this article. Watch this space though, I’m planning to cover Adapters in future posts.
Do you mind if I quote a few of your posts as long as I provide
credit and sources back to your blog? My blog
site is in the very same niche as yours and my users would certainly benefit from some of the information you provide here.
Please let me know if this ok with you. Thanks a lot!
The license for the articles on the site can be found here. Provided your site is non-commercial, your articles are published under the same licence terms, and you provide correct attribution then please feel free to quote my articles.
Thank you sooo much for this tutorials !… Hope that on July I buy a new computer and start programming in Android… Meanwhile I am collecting jewells like your site !… Cheers from Córdoba, Argentina (a province from the center of the country)…