I have an Activity defined by:
<LinearLayout
android:id="@+id/TabContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="99"
android:orientation="vertical">
<TabHost
android:id="@+android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/TabLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@+android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@+android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"></FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Basically it's a TabHost with TextViews as contents. I'm building the tabs and the content of those TextViews dynamically with createTabContent(). That works fine. But under some circumstances (some events processed by an AsyncTask with a TCP socket), I need to change the content of the some of those TextViews.
I'm convinced my problem is the result of my inexperience with Android SDK, but I can't find a way to iterate over the TabHost and get the content for each of those TextViews.
The "closest" approach I have achieved is:
TabHost th = (TabHost) findViewById(android.R.id.tabhost);
TabWidget tw = th.getTabWidget();
for (int i = 0; i < tw.getChildCount(); i++) {
LinearLayout lLayout = (LinearLayout) tw.getChildAt(i);
TextView tv = ((TextView) lLayout.getChildAt(i));
tv.append(linea);
}
But what this does is to append that "linea" to the name of the tab (to the Indicator). I want to add it to the content of that tab, but I can't find a way to retrieve the TextView associated to it.
Any ideas will be very appreciated!
--------- EDIT ---------
Although in a comment below I posted that I already had a solution, I found out it's not valid. The code looks now like this:
for (int i = 0; i < tw.getChildCount(); i++) {
LinearLayout LLayout = (LinearLayout) tw.getChildAt(i);
TextView currenttab = (TextView) LLayout.getChildAt(0);
if (tab.equals(currenttab.getText())) {
th.setCurrentTab(i);
TextView tabContent = (TextView) th.getTabContentView().getChildAt(0);
tabContent.append(linea);
return true;
}
}
The problem is that getTabContentView() references only to the active Tab at that moment, so if I want to add some text to a non-selected tab's TextView, I have to select it previously with .setCurrentTab(i), what means that I'd be changing the active Tab...
I think this has to be a lot easier than I'm thinking about... Any help with this?
Thanks!
--------- EDIT ---------
At that point I've realized that accessing an inactive tab's contents is not possible. At least not that way. Supposedly you cannot access an inactive tab's view because it has not an associated view, at least not a public one.
I've tried to declare a Map, where the first String is the tab's name, and the second is the TextView associated to its contents, and assign them in the moment of the tab creation. The result was that I was only able to reference the TextView of the active tab, the other ones threw a NullPointer exception.
So I focused the problem differently. Now if I have to add some text into an inactive tab, I add that text to a Hash, I declared an OnTabChangeListener and each time I change a tab I dump the pending content into it.
It's not the most brilliant solution, but it's the only one I got working... so if someone has a better solution to this I'll be grateful.