20

This seems like it should be simple, but I can't figure out a way to do it. I'm needing a tab to have beginning text, but then have that text change after the user selects an item from a list. I know how to change tab backgrounds and colors via

mTabHost.getChildAt(index).setBackgroundColor();

but there isn't an option to change the tab's indicator. I've tried using an EditText.

private EditText tabName;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.statistics);

    comm = new Communicator();

    tabName = new EditText(this);
    tabName.setText("BeginningText");

    mTabHost = getTabHost();
    mTabHost.addTab(mTabHost
                   .newTabSpec("tab_1_stat")
                   .setIndicator(User)
                   .setContent(R.id.meStatsTab));
    mTabHost.addTab(mTabHost
                   .newTabSpec("tab_2_stat")
                   .setIndicator(tabName.getText())
                   .setContent(R.id.themStatsTab));
    mTabHost.addTab(mTabHost
                   .newTabSpec("tab_3_stat")
                   .setIndicator("Archive")
                   .setContent(R.id.archiveStatsTab));
    mTabHost.setOnTabChangedListener(this);
    getTabWidget().getChildAt(1).setOnClickListener(new onThemTabClicked());
    mTabHost.setCurrentTab(0);

    onTabChanged("tab_1_stat");

}

.....

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    tabName.setText("ChangedTabText");
    Bundle extras = intent.getExtras();
    themStats = extras.getStringArray("themStats");
    mTabHost.setCurrentTab(1);
    onTabChanged("tab_2_stat");
}

That didn't work either, along with a few other attempts. Any ideas? Thanks ahead of time!

Honeal
  • 281
  • 1
  • 2
  • 7

7 Answers7

19

try this easiest way which works for me :

tabLayout.getTabAt(index).setText("TabName");
Ajay Mistry
  • 951
  • 1
  • 14
  • 30
8

Wow. Okay this was a pain. Apparently TabWidget does something funky with RelativeLayout and everytime I tried to do anything with radek-k's solution it was blowing up with a RelativeLayout error. So basically the work around is the following. It also allows you to change the font, font size, font color, etc.

TabWidget vTabs = getTabWidget();
RelativeLayout rLayout = (RelativeLayout) vTabs.getChildAt(tabIndex);
((TextView) rLayout.getChildAt(textIndex)).setText("NewTabText");

or in one line...

((TextView)((RelativeLayout)getTabWidget().getChildAt(tabIndex)).getChildAt(textIndex)).setText("NewTabText");

where "textIndex" is the index of the text field. In this case it is 1. If the tab has an icon or custom modifications the index could change.

Thanks again to radek-k. You definitely got me pointed in the right direction.

Honeal
  • 281
  • 1
  • 2
  • 7
  • If you're using the standard `TabActivity`, you can use `findViewById(android.R.id.title)` in place of `getChildAt(textIndex)` as in [this answer](http://stackoverflow.com/a/5577979/165674). – Dheeraj Vepakomma Jun 17 '12 at 13:16
5

You can do it without knowing the magic index of the TextView by using findViewById:

TabWidget vTabs = getTabWidget();
View indicatorView = vTabs.getChildAt(tabIndex);
((TextView) indicatorView.findViewById(android.R.id.title)).setText("NewTabText");
DMan
  • 191
  • 2
  • 4
3

Here is your layout:

<com.google.android.material.tabs.TabLayout
    android:id="@+id/ref_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.tabs.TabItem
        android:id="@+id/ref_book"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/ref_chpter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/ref_verse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</com.google.android.material.tabs.TabLayout>

Here is your code:

TabLayout tabLayout = findViewById(R.id.ref_tabs);
tabLayout.getTabAt(0).setText("BOOK"));
tabLayout.getTabAt(1).setText("CHAPTER"));
tabLayout.getTabAt(2).setText("VERSE"));
Aleksandar G
  • 1,163
  • 2
  • 20
  • 25
2
TabWidget vTabs = .....;

// get desired tab view  
View vTab = vTabs.getChildAt(i);

// I guess vTab is instance of TextView
TextView vText = (TextView) vTab;

vText.setText(...);
plugmind
  • 7,926
  • 4
  • 34
  • 39
  • I was able to play with that a little last night, but it's still blowing up on me. Have you got that to work? I'm going to experiment with it a little more today, but if you have a little more insight that'd be great. Thanks again. – Honeal Sep 09 '10 at 13:45
0

You can do it like this

TabHost tabHost = getTabHost();
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
tv.setText("New Tab Text");

Where android.R.id.title is System generated, just change ChildIndex and Text according to your need

Azhar
  • 20,500
  • 38
  • 146
  • 211
0

This is simple and easy. Try it. It works.

TabWidget tab = (TabWidget) findViewById (R.id.tab1);
((TextView)tab.getChildTabViewAt (1)).setText ("New name here");
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Lee Chu
  • 41
  • 3