Using Sherlock Actionbar to change tabs with a List Navigation in the Actionbar in Android, or switch between Activities with a List in the Actionbar.

Long title, I know.

So you have a TabHost and a TabWidget. You also are implementing Actionbars to give your app a more mobile-feel and interaction to it, so actual tabs aren’t that neat. However, tabs offer switching between Activities in a easy way, that works for you, or whatever your reasoning is.

So, you have the Actionbar up top, and you want to implement it so that the user doesn’t switch via tabs, but rather via a list. If you’re here, you probably know what I’m talking about. If not, then it’s a dropdown list that on item selected, it displays a new activity in the area below the Actionbar. I think I just repeated the same thing three or four times.

Anywys, I had a TabHost with a TabWidget and what not, all working fine. Looked okay, just it didn’t flow with the gameplay view. So I set out to change this menu to use an Actionbar and list.

Problem being, SherlockActivity extended Activity, and not ActivityGroup, which as some may know, is the known way to have the whole tab shebang working. So the problem arises that, if you change your Menu that did extend ActivityGroup to the SherlockActivity, you have TabHost.setup(LocalActivityManager). This problem is easily solved by a Google that you use:

		th = (TabHost) findViewById(;
		LocalActivityManager mLocalActivityManager = new LocalActivityManager(
				this, false);

In the onCreate with savedInstanceState being the Bundle that you pass it.

So no big deal so far. This works perfectly fine really. Then you add in the needed List requirements from Sherlock:

		Context context = getSupportActionBar().getThemedContext();
		ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(
				context, R.array.mainmenu, R.layout.sherlock_spinner_item);

		getSupportActionBar().setListNavigationCallbacks(list, this);

R.array.mainmenu is in my XML file in res/values/array.xml. You can Google this as well.

Finally, in the method you implement from the interface: ActionBar.OnNavigationListener, onNavigationItemSelected(int itemPosition, long itemId), you can do a simple:

		if (itemPosition < th.getTabWidget().getTabCount()) {
			return true;
		return false;

So really, pretty easy. Nothing too big, it’s useful as it allows you to switch between Activities with a List, also though, without using Fragments (Which I assume is the “correct” way to do this), or to have custom Views and what not that you change between.

Also, if you want that dirt Tab bar to disappear you want to add to the XML of the TabWidget:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s