How to get the content of a Tab within a TabHost?
Asked Answered
H

4

9

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.

Hemicellulose answered 17/11, 2013 at 12:0 Comment(0)
R
1

@nKn, thanks for getTabContentView(). In my case it returns not a tab, but a FrameLayout containing all tabs, and the following works:

View tab = tabHost.getTabContentView().getChildAt(2);
ImageView image = (ImageView) tab.findViewById(R.id.image);
Recap answered 16/11, 2016 at 18:7 Comment(0)
I
0

Tabs are accessible through TabWidget:

yourTabHost.getTabWidget().getChildTabViewAt(position);

This code returns the view of any tab, even an inactive one.

Immaculate answered 30/10, 2014 at 7:5 Comment(1)
In my case it returns a RelativeLayout without needed controls.Recap
C
0

First get the view of the entire tab with tabHost.getChildAt(n) and then do findViewById within the tab view to apply a change.

Example:

View tabView = tabHost.getChildAt(0);
View myView = tabView.findViewById(R.id.myView);
myView.setVisibility(View.INVISIBLE);
Clower answered 31/1, 2015 at 20:50 Comment(0)
F
0

TabHost child tab views can be accessed via TabWidget method getChildTabViewAt(position)

    TabWidget tabWidget = tabHost.getTabWidget();

    for (int i = 0; i < tabWidget.getTabCount(); i++) {

        View parentView = tabWidget.getChildTabViewAt(i);

    }

And selected tab view can be accessed by:

View parentView = tabHost.getCurrentTabView();
Fadiman answered 12/2, 2018 at 5:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.