Modify tab indicator dynamically in Android
Asked Answered
G

3

13

My application needs to update tab indicator dynamically, I'm trying to do this by invoke TabSpec.setIndicator(), but it doesn't work. Here is my code:

In onCreate method of TabActivity:

tabHost = getTabHost(); 
TabSpec tabSpec = tabHost.newTabSpec("abc");
tabSpec.setIndicator("helloabc");
tabSpec.setContent(new MyViewFactory());
tabHost.addTab(tabSpec);

Now I need to change tab indicator to another string, for example, "xyz"

TabSpec tabSpec = MyTabActivity.getTabSpec();
tabSpec.setIndicator("xyz");

But it doesn't work. So I'd like to know how to change tab indicator after it is added to the tabhost? Many thanks.

Solution

Thanks to @CommonsWare, I make it by using TabWidget.getChildAt, here is my code for your convenience:

TextView title = (TextView) tabHost.getTabWidget().getChildAt(tabId).findViewById(android.R.id.title)
title.setText("xyz");
Goren answered 29/5, 2010 at 16:26 Comment(1)
can i change the image indicator this way?Taxiway
T
13

I apologize for my earlier incorrect answer, now deleted.

Try using getChildViewAt() on TabWidget.

Trogon answered 29/5, 2010 at 22:6 Comment(1)
How can i change the image indicator of tab bar?Taxiway
F
7

i managaed with getChildTabViewAt(tabId) instead of childviewAt(id) for multiple tab.

Forcible answered 13/12, 2011 at 11:9 Comment(0)
F
0

Try this:

 TextView title = (TextView) TabHost.getTabWidget().getChildTabViewAt(tabId).findViewById(android.R.id.title);
Ferdinande answered 30/8, 2013 at 5:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.