Search code examples
androidandroid-studiomaterial-designandroid-tabs

How to set color for both text and icon of a tab in TabLayout when it gets focus


Here is my activity_custom_view_icon_text_tabs.xml layout :

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="@dimen/custom_tab_layout_height"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

Here is my code for activity_custom_view_icon_text_tabs.xml : http://pastebin.com/raw/h0AN6Mtj

custom_tab.xml :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tab"
android:textColor="@color/colorAccent"
android:textSize="@dimen/tab_label"
android:fontFamily="@string/font_fontFamily_medium"/>

Solution

  • i run this code in my example and it is working fine.

    you need to make some changes describribed in following example.

    for tab selector background under res/drawable/background_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/colorAccent" android:state_selected="true" />
        <item android:drawable="@color/tabcolor" />
    </selector>
    

    and for text selector color under res/color/background_text_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@android:color/holo_red_dark" android:state_selected="true" />
        <item android:color="@android:color/holo_green_dark" />
    </selector>
    

    and your textview is something like this:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_selector"
        android:textColor="@color/background_text_selector">
    </TextView>
    

    references took from for color selector:

    https://developer.android.com/guide/topics/resources/color-list-resource.html

    and for drawable selector:

    https://developer.android.com/guide/topics/resources/drawable-resource.html

    from this links.