views:

158

answers:

2

I'm aware that API 3 doesn't allow a view to be set for a tab, but I still need to modify the TextView displayed as the indicator. I'd also like to change the Drawable for the tab, but I don't see a way to do it without a custom tab view as allowed in 1.6, API 4.

Working with this generic example below, is there way to retrieve the TextView and modify its properties or change the drawable?

       TabHost tabHost = getTabHost();
       TabHost.TabSpec spec;

       spec = tabHost.newTabSpec("nearby")
                      .setIndicator("Nearby Activity")
                      .setContent(R.id.nearby_list);                
       tabHost.addTab(spec);        

       spec = tabHost.newTabSpec("friends")
                  .setIndicator("Friends & Favorites")
                      .setContent(R.id.friends_list);
       tabHost.addTab(spec);        

       tabHost.setCurrentTabByTag("nearby");

Thanks for any help.

+1  A: 

Since the Tabwidget is a normal ViewGroup I think you should access to the all its children. Something like this should work:

   tabHost.setOnTabChangedListener(this);
....
...


@Override
    public void onTabChanged(String tabId) {

        int count = tabHost.getTabWidget().getChildCount();
        for (int i = 0; i < count; i++){
            ViewGroup v = (ViewGroup) tabHost.getTabWidget().getChildAt(i); //Should be a relative layout
            TextView tv = (TextView)v.getChildAt(1); //At position 0 should be the ImageView
            Log.d("onTabChanged",tv.getText().toString());
        }

    }

Maybe, you have to play with the indexes position to find the right View.

Francesco
+1  A: 

to change the icon:

 .setIndicator("Friends & Favorites",getResources().getDrawable(R.drawable.myTabIcon))

to change the TabBackground Drawable::

tabHost.getTabWidget().getChildAt( index of tab ).setBackgroundResource(R.drawable.mytab_Newbackground);

to retrieve the Textview, check Francesco's sample

ViewGroup v = (ViewGroup) tabHost.getTabWidget().getChildAt( index of tab );
TextView tv = (TextView)v.getChildAt(1); 
Log.d("Tab's text = ",tv.getText().toString());
Jorgesys