Is it just me, or are Android tabs a real headache? There are tabs that host activities, tabs that host views, tab widgets, tab content providers, and now (apparently), Android developers have to worry about something called fragments. I'm a big fan of Android, but I'm not thrilled with a lot of the UI framework, and tabs are the worst offenders.
And yet there are times, when I can't make do with anything other than Android tabs. During those times, I grab a cup of coffee, pull up the Google code docs, and try out of respect for my coworkers to keep my cursing to a minimum. The last time I had to implement a tabbed UI, I decided what was really needed was a bare bones sample program. Something so minimalistic that regardless of how convoluted the tab layout hierarchy is one couldn't help but get it right.
My project had a deadline quickly approaching, and as has always proved the case for me, implementing my tabbed UI ate up more time than I'd allocated, so I shelved the tab tutorial for a rainy day. Fast forward several months to when a TechRepublic reader emailed me about doing a tab tutorial, and I thought it was a perfect time for me to dig into Android tabs and slay this dragon once and for all.
However, when I pulled up those same Google code docs I referenced previously, I was greeted by a big bold note informing me the TabActivity class had been deprecated. My initial reaction was one of utter exuberance. I assumed at last the Google UI framework team had gone back and pulled this thorn from my side.
I quickly discovered that the new recommended class for handling tabs is vastly more complicated than its predecessor (though it is more flexible, especially when it comes to tablets). In fact, the new mechanism is so much more involved that it requires users download a compatibility library into their development environment, and utilize a static inner class, which includes the following comment:
This is a helper class that implements a generic mechanism for associating fragments with the tabs in a tab host. It relies on a trick.
That's right. The official way of doing tabs now relies on "a trick." While there was an awkward period during my youth where I dreamed of being a magician, I gave up that career path about the time I discovered the pretty girl who lived two houses down from me preferred a boy on the soccer team to one who could produce 12 feet of knotted silk from a previously empty cardboard box.
So after doing a good bit of soul searching (about this tutorial not about becoming a magician), I decided to go ahead and demonstrate the "old" way of doing tabs. For those of you who say shame on me, I offer the following arguments:
- While the FragmentActivity works, it is simply too complex an implementation to cover in a single TechRepublic post.
- The tutorials I write for TechRepublic are aimed at beginners, and the FragmentsTabs sample provided by Google could send developers not familiar with the workings of the now deprecated TabActivity running for cover (or worse, an iPhone).
- The "new" way of doing tabs is targeted at devices running Honeycomb and above, which according to Google's own dashboard accounts for less than 1.5% of the market.
I'm not saying don't bother to learn how to use the Fragment classes -- if you want to develop for Android, eventually you will not have a choice -- I'm saying that learning how the deprecated TabActivities work will get you up and running faster. Another advantage to learning the "old" way is that you'll gain knowledge you'll be able to use when you move on to the new way of doing things. (Right now, a lot of what is handled automatically for the developer using the TabActivity class has reverted back to manual handling in the FragmentActivity class.)
This tutorial can get your tabs up and running quickly on 98.5 % of the devices people have today; it will give you the background you'll need to code something that works for the other 1.5% of potential users; and, ice cream sandwich is just around the corner, and some improvements are likely forthcoming in this area.
You can work through the tutorial that follows, or download the project here.Go to page two to see the tutorial.
William J Francis began programming computers at age eleven. Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intelligence Corps. Throughout his career William has published numerous technical articles, as well as the occasional short story.