Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabLayout (Android Design Library) Text Color

I'm using the new TabLayout from the Android Design library. I managed to set the textcolor statelist using tabLayout.setTabTextColors(colorstatelist)

How can i achieve the same using styles.xml?

like image 647
sebastian Avatar asked Jun 18 '15 07:06

sebastian


2 Answers

Via XML attributes:

<android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabTextColor="@color/your_unselected_text_color"
        app:tabSelectedTextColor="@color/your_selected_text_color"/>

Additionally, there are attributes like tabIndicatorColor or tabIndicatorHeight for further styling.

In code:

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

Since this old way is deprecated as of API 23, the alternative is:

tabLayout.setTabTextColors(
    ContextCompat.getColor(context, R.color.your_unselected_text_color),
    ContextCompat.getColor(context, R.color.your_selected_text_color)
);
like image 150
Fe Le Avatar answered Nov 10 '22 04:11

Fe Le


Here is snippet code to override text style and selected text color

<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 snippet code for layout

<android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/MyCustomTabLayout" />
like image 81
Chuehnone Avatar answered Nov 10 '22 05:11

Chuehnone