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(R.id.thMain);
		LocalActivityManager mLocalActivityManager = new LocalActivityManager(
				this, false);
		mLocalActivityManager.dispatchCreate(savedInstanceState);
		th.setup(mLocalActivityManager);

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);
		list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		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()) {
			th.setCurrentTab(itemPosition);
			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:

            android:visibility="gone" 
Advertisements