Search code examples
androidandroid-tablayout

Change tab font size when selected/unselected in TabLayout Android


I want to change my tab text size when selected or unselected like this. enter image description here

My tablayout

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/toolbar"


    android:overScrollMode="never"

    app:tabGravity="fill"
    app:tabMaxWidth="0dp"
    app:tabMode="scrollable"

    app:tabRippleColor="@null"

    app:tabTextAppearance="@style/MyCustomTabText"

    app:tabIndicatorHeight="0dp"

    />

MyCustom style

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyCustomTabText">
    <item name="android:textSize">16sp</item>
</style>
<resources>

I use the custom style for text size but I do not know how to change when it is selected/unselected. Could someone give me a hand with this, please?


Solution

  • To change the Tab font size based the selected/unselected state you have to use your custom tab view and use TabLayout.OnTabSelectedListener to change the size of selected/unselected tab.

    1.TabLayout in xml can be like below:

    <com.google.android.material.tabs.TabLayout
      android:id="@+id/tabLayout"
      android:layout_width="match_parent"
      android:layout_height="60dp"
      app:tabGravity="fill"
      app:tabIndicatorColor="@android:color/holo_orange_light">
    
         <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab 1" />
    
         <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab 2" />
    
         <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab 3" />
    
    </com.google.android.material.tabs.TabLayout>
    

    2.Initialise for each tab a CustomView using setCustomView method and use TabLayout.OnTabSelectedListener to listen which tab is currently selected and which one is now unselected and change the text size like below:

    //get each tab from tabLayout
    TabLayout.Tab tab0 = tabLayout.getTabAt(0);
    TabLayout.Tab tab1 = tabLayout.getTabAt(1);
    TabLayout.Tab tab2 = tabLayout.getTabAt(2);
    
    //and set for each one a custom View
    tab0.setCustomView(createCustomTabView("Tab 0", 30, android.R.color.holo_green_light)); //initially this tab is selected
    tab1.setCustomView(createCustomTabView("Tab 1", 15, android.R.color.black));
    tab2.setCustomView(createCustomTabView("Tab 2", 15, android.R.color.black));
    
    //add OnTabSelectedListener
     tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
      @Override
      public void onTabSelected(TabLayout.Tab tab) {
         setTabTextSize(tab, 30, android.R.color.holo_green_light);
      }
      @Override
      public void onTabUnselected(TabLayout.Tab tab) {
         setTabTextSize(tab, 15, android.R.color.black);
      }
      @Override
      public void onTabReselected(TabLayout.Tab tab) {
      }
    });
    

    with the below helper functions to create the Tab CustomView and to change the text size based the selected/unselected state:

    private View createCustomTabView(String tabText, int tabSizeSp, int textColor){
    
      View tabCustomView = getLayoutInflater().inflate(R.layout.tab_customview, null);
      TextView tabTextView = tabCustomView.findViewById(R.id.tabTV);
      tabTextView.setText(tabText);
      tabTextView.setTextSize(tabSizeSp);           
      tabTextView.setTextColor(ContextCompat.getColor(tabCustomView.getContext(), textColor)); 
      return tabCustomView;
    }
    
    private void setTabTextSize(TabLayout.Tab tab, int tabSizeSp, int textColor){
    
        View tabCustomView = tab.getCustomView();
        TextView tabTextView = tabCustomView.findViewById(R.id.tabTV);
        tabTextView.setTextSize(tabSizeSp);
        tabTextView.setTextColor(ContextCompat.getColor(tabCustomView.getContext(), textColor));
     }
    

    3.And the custom Tab layout R.layout.tab_customview can be like this:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:tools="http://schemas.android.com/tools">
    
        <TextView
            android:id="@+id/tabTV"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:textSize="15sp"
            android:textAlignment="center"
            android:textColor="@android:color/black"
            android:maxLines="1"
            tools:text="Tab"/>
    
    </RelativeLayout>
    

    Result:

    tablayout_textsize