How to change android tab text on the fly?

后端 未结 7 1666
灰色年华
灰色年华 2021-01-04 01:42

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

相关标签:
7条回答
  • 2021-01-04 02:21

    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");
    
    0 讨论(0)
  • 2021-01-04 02:26

    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

    0 讨论(0)
  • 2021-01-04 02:32

    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.

    0 讨论(0)
  • 2021-01-04 02:46

    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"));
    
    0 讨论(0)
  • 2021-01-04 02:46
    TabWidget vTabs = .....;
    
    // get desired tab view  
    View vTab = vTabs.getChildAt(i);
    
    // I guess vTab is instance of TextView
    TextView vText = (TextView) vTab;
    
    vText.setText(...);
    
    0 讨论(0)
  • 2021-01-04 02:47

    try this easiest way which works for me :

    tabLayout.getTabAt(index).setText("TabName");
    
    0 讨论(0)
提交回复
热议问题