Search code examples
androidlayoutandroid-tablayout

How to Change Text Color of tab Layout?


I have this code for change color of text of tab layout, but it does not work at all!

app:tabTextColor does not work, and it cant change color to white.

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:tabIndicatorColor="@color/blue"
        app:tabIndicatorHeight="5dp"
        app:tabTextColor="@color/white" />

Solution

  • You can customize your TabLayout's text.

    Create a TextView from Java Code or XML like this

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:textSize="15sp"
        android:textColor="@color/tabs_default_color"
        android:gravity="center"
        android:layout_height="match_parent"
    />
    

    Make sure to keep the id as it is here because the TabLayout check for this ID if you use custom TextView

    Then from code inflate this layout and set the custom Typeface on that TextView and add this custom view to the tab.

    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        //noinspection ConstantConditions
     TextView tv=(TextView)LayoutInflater.from(this).inflate(R.layout.custom_tab,null)
     tv.setTextColor(customColor)
     tabLayout.getTabAt(i).setCustomView(tv);
    
    }