Andreas
Andreas

Reputation: 10067

How can I insert a tab at an arbitrary position instead of the last?

I can add a new tab to a TabHost by using the addTab() method. But this will always add the tab as the last tab. How can I add tabs in other positions than the last in the TabHost? For example, how can I insert a new tab as the very first tab or into other arbitrary positions?

I don't want to use XML but everything should be done programmatically.

One solution that comes to my mind is to first remove all tabs by calling clearAllTabs() on the TabHost and then re-add all tabs in the order I want to have them. But this is of course a very hackish solution. Isn't there a nicer one?

Upvotes: 1

Views: 73

Answers (1)

Gennadii Saprykin
Gennadii Saprykin

Reputation: 4573

Apparently there is not. If you look at the TabHost source code, the only way to add something to mTabSpecs is via the addTab method. Extending the class won't help either since the list is private.

So, as you said, using clearAllTabs() is the least 'hacky' way of doing it. I think the use case itself is non-trivial, so I can understand why achieving the behavior is not that straightforward. But I wouldn't call it a "very hacky" solution. There is no java reflection going on, it's simply invalidating the view container and adding a new list of views to it. It won't impact performance, nor memory, nor readability, nor it will break if the SDK updates or something like that. So I'm pretty sure it's totally fine.

Upvotes: 1

Related Questions