tags:

views:

129

answers:

2

I have a need to create a control similar to UITabBar in iPhone, which is to be present on every activity of my application. UITabBar essentially is a battery of buttons exhibiting a TAB like behavior: every button maps to an activity. I have two solutions for this:
1. In the layout XML for every activity, I insert a <LinearLayout><Button/><Button/><Button/></LinearLayout> element. And then have a common listener class that will handle the button clicks. So, every activity will have an instance of this listener.
2. To create a custom Widget extending LinearLayout class, put all the buttons as its static members and let it handle the button clicks. Include this custom control in every screen.

I am not sure which approach to follow. Please advice. Alternatives also appreciated

Following is what I think about this:
The problem with first approach:

1. it will generate a lot of boiler plate code(findViewByIds, setOnClickListener etc.)
2. Assuming that there are 5 activities and 3 tab buttons, total number of Button objects created at Runtime will be 5 x 3 = 15

I'd like to take the 2nd approach because:
1. All the code(state and behavior) will be encapsulated by the widget class. Less Boilerplate code.
2. Since, the buttons will be static members, the total number of Button objects created at Runtime will be only three. Though, the static members will remain in memory for a longer time(until JVM unloads the class), since the control is present on every screen I think this can be excused.

Thanks.

+1  A: 

Why not just use the existing TabWidget implementation in Android?

http://developer.android.com/guide/tutorials/views/hello-tabwidget.html

Not only is implementing your own widget extra work, it's likely to feel out of place when compared with other apps on the platform.

In particular, here's some problems I see:

  1. When the look of the tab bar changes due to a system update, you'll have to re-write your code. You're also unlikely to exactly match the look and feel of the built in tab bar. (Think Java on OS X.)

  2. Every time a user clicks on a tab, it will add another activity to the stack. Not only is this a waste of memory, but every time the user clicks the back button on the phone, it will go to the previous tab. This isn't the way the tab bar is supposed to work.

Trevor Johns
As I said, I have a requirement to emulate TabBar; unfortunately, it is beyond my control to dictate/ask anything here. I don't think the look of the built-in TabWidget can be customized forasmuch as to look like iPhone's TabBar.Apparently, the folks do not care if the application looks different from the others.I think your second point can be handled to some extent by using appropriate Intent Flags(Reorder to front, Clear top etc.); though I must say it will be a bit complex.
Samuh
A: 

Unfortunately, I couldn't find a way to customize the built-in TabWidget to make it look like UITabBar. So, I am going to create a custom control and have the Buttons as static members(take the second approach). The control will handle the clicks as expected.
I've notice that the award winning Plink Art application too has a tab control that is similar to iPhones UITabBar.

Samuh