Search code examples
androidandroid-tablayout

set textcolor of tab


why am I not able to set the textcolor of the tablayout?

Here is the whole xml-source. The relevant line is:

 app:tabTextColor="@color/tabTextColor"

The text is not white - its black

And here is the whole source coude

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"


 xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/linlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/toolbar"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#4f83cc"
        app:tabMode="scrollable"
        app:tabIndicatorColor="#FFFFFF"
        app:tabTextColor="@color/tabTextColor"
        >
    </android.support.design.widget.TabLayout>


    <android.support.v4.view.ViewPager

        android:id="@+id/viewPager"
        android:layout_below="@+id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

and in the color-xml file:

 <?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="colorPrimary">#01579b</color>
   <color name="colorPrimaryDark">#002f6c</color>
   <color name="colorAccent">#FF4081</color>
   <color name="tabTextColor">#FFFFFF</color>
</resources>

Solution

  • You can chnage text color of TabLayout using java code

    tabLayout.setTabTextColors(
      getResources().getColor(R.color.your_unselected_text_color),
      getResources().getColor(R.color.your_selected_text_color)
    );
    

    Or You can try custom style for TabLayout

    <style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
      <item name="tabTextAppearance">@style/MyCustomTabText</item>
      <item name="tabSelectedTextColor">@color/tab_text_act</item>
    </style>
    
    <style name="MyCustomTabText" parent="TextAppearance.AppCompat.Button">
      <item name="android:textSize">14sp</item>
      <item name="android:textColor">@color/tab_text</item>
    </style>
    

    and here is code

    <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/MyCustomTabLayout" />