Smartphones optimize

Android tabs hurt my head

The official way of handling tabs in Android now relies on "a trick." William Francis makes the case for why he demonstrates the "old" way of doing tabs.

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.

About

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 Intellige...

4 comments
authorwjf
authorwjf

Sorry about that guys! Actually the compatibility library isn't really needed for this project. I just failed to start from a clean environment. Oops. Thanks @ dr_oopie though for pointing other readers to the JAR download. I will try and be more diligent when packaging up my source code downloads in the future.

dr_oopie
dr_oopie

all i did was follow this.. http://developer.android.com/resources/tutorials/views/hello-tabwidget.html to test it out and learn how to add icons, i just did exactly what step 3 says to do. jumped to step 6 in the main.java class and added.. Resources res = getResources(); // Resource object to get Drawables then changed ts1.setIndicator("First Tab").setContent(new Intent(this,tab1.class)); to ts1.setIndicator("First Tab",res.getDrawable(R.drawable.ic_tab_artists)).setContent(new Intent(this,tab1.class)); all it does is add ,res.getDrawable(R.drawable.ic_tab_artists) after "First Tab". works great!

dr_oopie
dr_oopie

its 2:00am on a saturday night, sunday morning. before going to bed on the weekends, i check the tech republic pages for new tech updates. i especially look forward to try out each weeks tutorial from William J. Francis. he is the greatest tutorial writer i have seen for android. this latest tutorial actually woke me up from laughing throughout the page. keep up the great work!