Change text on Android tab

I have an Android application with 3 tabs in TabHost (text labels, no images). I set up the tabs like this:

intent = new Intent().setClass(this, AnnouncementsActivity.class);
spec = tabHost.newTabSpec("news").setIndicator("News").setContent(intent);
tabHost.addTab(spec);

I am running a background thread to retrieve ads from my server, and I want to update the text label on the tab to tell the user how many new ads there are. For example, I want to change the text on the tab to "News (3)". How can I access and change the text label on a tab?

Any suggestions are welcome!

+3
source share
4 answers

Look at the code demonstrated here , see at the end of the code there is a hacker way to get a textView.

0
source

:

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

android.R.id.title , ChildIndex

+5

android (5.1) :

, . .

FragmentTabHost tabHost = (FragmentTabHost)view.findViewById(android.R.id.tabhost);
AppCompatTextView tv = null;
RelativeLayout parentLayout = (RelativeLayout)tabHost.getTabWidget().getChildAt(0);

for (int i = 0; i < parentLayout.getChildCount(); i++) {

    View tempView = parentLayout.getChildAt(i);

    if (tempView instanceof AppCompatTextView) {
        tv = (AppCompatTextView)tempView;
    }
}

if (tv != null) {
    tv.setText("Your updated text goes here");
}

, , . android.R.id.title , . , AppCompatTextView TextView.

, , android.

, !

+1

TabHost, , .

Please excuse my English, I'm Brazilian and I'm still studying. I hope to help someone who has the same problem.

TabHost host = (TabHost)findViewById(R.id.tabDetail);
    host.setup();

    //Tab 1
    TabHost.TabSpec spec = host.newTabSpec("tab1");
    spec.setContent(R.id.tab1);
    spec.setIndicator("Details");
    host.addTab(spec);

    //Tab 2
    spec = host.newTabSpec("tab2");
    spec.setContent(R.id.tab2);
    spec.setIndicator("Comments");
    host.addTab(spec);
0
source

Source: https://habr.com/ru/post/1768780/


All Articles