5

I'm trying to get use to Android Honeycomb by creating a simple text editing application which utilizes the Action Bar and tabs. I am running into an annoying issue though. After a tab has been created and added to the Action Bar I would like to change the text displayed on the tab. I thought that using the following method, ActionBar.Tab.setText(CharSequence arg0) would do the trick, however, it doesn't seem to be changing the viewable text. What's weirder still is that if I were to call getText() it returns the text that I changed the tab to. Below is a snippet of code that I am using to change the tab text:

int currentTabIndex = ab.getSelectedNavigationIndex();
currentTabTitle = (String) ab.getTabAt(currentTabIndex).getText();  // just to check
ab.getTabAt(currentTabIndex).setText(fileName);                     // change tab text
currentTabTitle = (String) ab.getTabAt(currentTabIndex).getText();  // just to check

I really am at a loss and have searched everywhere. I would greatly appreciate any advice that anyone has. Thanks for your time.

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
Deeek
  • 51
  • 2
  • Internet searchers, please vote for this issue: http://code.google.com/p/android/issues/detail?id=17129&q=actionbar&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars – xandy Oct 05 '11 at 04:46

2 Answers2

10

This is kind of a silly issue and adding and removing tabs is a bad idea because if you're using fragments you will end up removing and re-adding your fragment with its tab. Using a custom view seems to work much better and as an added bonus offers you greater customization.

Here's how to make a tab with a custom view that looks and behaves identical to the default ones:

ActionBar bar = getActionBar();

TabListener tabListener = new TabListener() {

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
};

Tab tab1 = bar.newTab()
          .setText("Info")
          .setTabListener(tabListener)
          .setCustomView(makeTabDummy("Info", android.R.drawable.ic_menu_info_details));

bar.addTab(tab1);

and here is the pixel perfect dummy view:

private TextView makeTabDummy(String text, int icon) {

    TextView tv = new TextView(this);
    tv.setText(text);
    tv.setTextColor(0xffffffff);
    tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
    tv.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
    tv.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
    tv.setGravity(Gravity.CENTER);

    return tv;
}

From here we can change icons and text on the tab without any problems at all. Example:

TextView tv = (TextView) tab1.getCustomView();          
tv.setText("change the text!");
tv.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.btn_star_big_on, 0, 0, 0);

... and everything works as it should

ckozl
  • 6,751
  • 3
  • 33
  • 50
  • I see this problem after orientation change in compatibility package ( Api 18), so will use this solution. – Divers Oct 14 '13 at 10:27
0

Try removing the tab and re-adding it at the desired index after changing the text. (It's a bug. The associated view doesn't update when you set the text after adding.)

adamp
  • 28,862
  • 9
  • 81
  • 69