Retrieving TabLayout Tab Text

I want to programmatically change the text representation of a tab. Is there any way to do this?

There are answers only regarding the old TabHost view, I use TabLayout, used by the Google Material Design material library, using android.support.design.widget.TabLayout.

For TabHost: How to add an addition to the tab shortcut?

+5
source share
6 answers

Using this tab:

  int wantedTabIndex = 0; TextView tv = (TextView)(((LinearLayout)((LinearLayout)mTabLayout.getChildAt(0)).getChildAt(wantedTabIndex)).getChildAt(0)); tv.setText("Hello world!"); 
+3
source

This is a working solution.

 int wantedTabIndex = 0; TextView tv = (TextView)(((LinearLayout)((LinearLayout)mTabLayout.getChildAt(0)).getChildAt(wantedTabIndex)).getChildAt(1)); tv.setText("Hello world!"); 

Just change the last index Zero to One , now the code will work on

Crash removed

 java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView 
+10
source
  public static void setTextViewsCapsOff(View view) { if (!(view instanceof ViewGroup)) { return; } ViewGroup group = (ViewGroup)view; for (int i = 0; i < group.getChildCount(); i++) { View child = group.getChildAt(i); if (child instanceof TextView) { ((TextView)child).setAllCaps(false); } else { setTextViewsCapsOff(child); } } } 

Go to your TabLayout to this recursive method. It will find any child TextView object and disable its All Caps mode. Avoids all other very specific types. If it does not work, refer to it later in your code. I had this in onCreate, but it did not work. Called later in the code, and it works great.

Affects all tabs, not just one, but I think this is the most common use. Not specific to TabLayout. Can be used for any layout containing TextViews that you want to change.

+2
source

I think the best thing that can be done here is to create your own layout using a TextView, and then assign a custom view to the tabs you want to edit.

 final TabLayout.Tab tabAt = tabLayout.getTabAt(index); tabAt.setCustomView(myLayoutId); 

This way you can do whatever you want with the TextView, which is inside the layout.

0
source

Add onTabSelectedListener to tabLayout to dynamically set text. therefore, in this approach, when you select a tab, the text will become β€œselected” or another tab

  tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { tab.getCustomView().setAlpha(1.0f); ((TextView) tab.getCustomView()).setTextSize(12); ((TextView) tab.getCustomView()).setText("Selected"); Log.d("TabBar", "selected"); } @Override public void onTabUnselected(TabLayout.Tab tab) { tab.getCustomView().setAlpha(0.3f);//to set alpha ((TextView) tab.getCustomView()).setTextSize(11); ((TextView) tab.getCustomView()).setText("DeSelected"); Log.d("TabBar", "unselected"); } @Override public void onTabReselected(TabLayout.Tab tab) { Log.d("TabBar", "reselected"); } }); 

to set the default tab, then use this statement

 tabLayout.getTabAt(0).select(); //this is used to set tab as default 
0
source

This worked for me:

 tabLayout.getTabAt(0).setText("some text"); 

Replace 0 pointer of the tab you want to edit. I think your tabLayout should be filled before calling this, otherwise tablayout.getTabAt(0) will return null .

0
source

All Articles