views:

35

answers:

2

Is it possible to update the TabWidget icons/indicators? If so, how, when you are creating TabContent through intents?

Edits below with answer thanks to @Bart

Generic code:

MainActivity:

public class TestActivity extends TabActivity {

public int i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);

    TabHost mTabHost = getTabHost();

    mTabHost.addTab(mTabHost.newTabSpec("tab1").setContent(
            new Intent(this, OneActivity.class)).setIndicator("One"));
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setContent(
            new Intent(this, TwoActivity.class)).setIndicator("Two"));

    changeTitle(0, 20);
}

public void changeTitle(int cnt, int i){
    View v = getTabWidget().getChildAt(cnt);
    TextView tv = (TextView) v.findViewById(android.R.id.title);
    tv.setText("One ("+i+")");
}

}

OneActivity (ChildActivity):

public class OneActivity extends Activity {
TextView tv;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.one);

    tv = (TextView) findViewById(R.id.tv);
    btn = (Button) findViewById(R.id.btn);

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            int i = Integer.parseInt((String) tv.getText());
            i++;
            tv.setText(""+i);

             TestActivity parent = OneActivity.this.getParent();
             parent.changeTitle(0,i);

        }
    });
}

}

I'd like to show the number in the Tab's title

+1  A: 

It is possible but it is a bit of tricky:

ViewGroup vg = (ViewGroup) getTabHost().getTabWidget().getChildAt(0);
TextView tv = (TextView) vg.getChildAt(1);

You should pay attention to indices while calling getChildAt(). The best way is always to debug and check.

Since 1.6 TabWidget has a method getChildTabViewAt thanks to which you could skip the first line and write:

ViewGroup vg = (ViewGroup) getTabHost().getTabWidget().getChildTabViewAt(0);




I attach also source code of Android SDK to show how TabWidget icons are built:

View tabIndicator = inflater.inflate(R.layout.tab_indicator, 
mTabWidget, // tab widget is the parent
false); // no inflate params

final TextView tv = (TextView) tabIndicator.findViewById(R.id.title);
tv.setText(mLabel);
final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.icon); 
iconView.setImageDrawable(mIcon);

As you can see, it contains a TextView and ImageView.

bart
Note that your "source code of Android SDK" is for some specific version, and that code may change, and so you do not want to rely upon that implementation.
CommonsWare
How do I pass the value from OneActivity to MainActivity (extends TabActivity)?
Sameer Segal
I agree that using getChildAt() may not work on every SDK. I wrote that it is tricky. It will work for sure in versions from 1.6-2.1 since I tested it.@Sameer Segal: you can get a reference to MainActivity from OneActivity by calling TabActivity mainActivity = (TabActivity) oneActivity.getParent();
bart
@bart, I think I am missing a step here. I've updated the code on top and am not able to find a function OneActivity.getParent().
Sameer Segal
Figured it out -- thanks Bart! Updated my question with the answer.
Sameer Segal
A: 

Given a arrays for titles and icons

    for (int i = 0; i < numTabs; i++ ) {
        TabSpec spec = host.newTabSpec(String.valueOf(i)); //sets tab index
        Drawable icon = res.getDrawable(icons[i]);  //sets the icon
        // set tab text and icon
        spec.setIndicator(titles[i], icon);  //sets the title

} host.addTab(spec);

Jim