Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot change active text color on TabLayout

Tags:

android

I am trying to style tabs in an android.support.design.widget.TabLayout I can not get the selected tab color to change, it is always set to the textColorPrimary in my app theme, but I need them to be different colors.

I have tried setting values in styles.xml that applies to TabLayout, but I read you can not change active tab text color this way, though I can change unselected tab text colors. I have also tried:

tabLayout.setTabTextColors(getResources().getColorStateList(R.color.selector));

and

tabLayout.setTabTextColors(R.color.Green, R.color.Blue);

Is it possible to override the selected tab text color?

like image 781
Xeridea Avatar asked Jun 18 '15 17:06

Xeridea


3 Answers

Edit: got it working,

tabLayout.setTabTextColors(getResources().getColorStateList(R.color.selector));

needed called before it was attached to the view pager

like image 144
Xeridea Avatar answered Oct 24 '22 08:10

Xeridea


Actually you CAN customize active tab text color via defining custom TabLayout style. Look at tabSelectedTextColor parameter. Here is example of customizing tabSelectedTextColor, tabIndicatorColor, tabTextAppearance (text size/color etc):

<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/CustomTabLayoutStyle"/>

Styles:

<style name="CustomTabLayoutStyle" parent="Base.Widget.Design.TabLayout">
    <item name="tabSelectedTextColor">@color/tab_text_selected</item>
    <item name="tabIndicatorColor">@color/tab_indicator</item>
    <item name="tabTextAppearance">@style/CustomTabTexStyle</item>
</style>

<style name="CustomTabTexStyle" parent="TextAppearance.Design.Tab">
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">@color/tab_text</item>
    <item name="textAllCaps">false</item>
    ...
</style>
like image 27
ashakirov Avatar answered Oct 24 '22 07:10

ashakirov


Add below code to your xml:

app:tabSelectedTextColor="@color/app_color"
like image 8
veeson Avatar answered Oct 24 '22 09:10

veeson