Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change android tab text on the fly?

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!

like image 416
Honeal Avatar asked Sep 08 '10 12:09

Honeal


People also ask

How do I use TabLayout on Android?

This example demonstrates how do I create a Tab Layout in android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 3 − Add the following code to res/layout/activity_main. xml.

How do I add a tab on Android?

Tabs are created using newTab() method of TabLayout class. The title and icon of Tabs are set through setText(int) and setIcon(int) methods of TabListener interface respectively. Tabs of layout are attached over TabLayout using the method addTab(Tab) method.


1 Answers

try this easiest way which works for me :

tabLayout.getTabAt(index).setText("TabName");
like image 193
Ajay Mistry Avatar answered Sep 20 '22 02:09

Ajay Mistry