Search code examples
androidandroid-viewpagerandroid-tablayout

TabLayout color of unselected tab underline


enter image description here

In this picture, in tablayout, selected tabbar underline color is purple, and text.

I search of unselected tabbar, but I couldn't find unselected tabbar underline.

I want change the color when I select some tab, change the unselected tabbar underline color.

If you know about this, would you help me?


Solution

  • Create an xml file inside your drawable folder

    custom_indicator.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <!-- UNSELECTED TAB STATE -->
    <item android:state_selected="false" android:state_pressed="false">
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <!-- Bottom indicator color for the UNSELECTED tab state -->
            <item android:top="-5dp" android:left="-5dp" android:right="-5dp">
                <shape android:shape="rectangle">
                    <stroke android:color="#65acee" android:width="2dp"/>
                </shape>
            </item>
        </layer-list>
    </item>
    </selector>
    

    And set this drawable in your tabLayout

    <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                app:tabGravity="fill"
                app:tabMode="fixed"
                app:tabBackground="@drawable/custom_indicator" />
    

    To change the unselected tab text color, simple provide a default tab text color and selected tab text color as follows:

    <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                app:tabGravity="fill"
                app:tabMode="fixed"
                app:tabTextColor="@color/unselected_color"
                app:tabSelectedTextColor="@color/selected_color"
                app:tabBackground="@drawable/custom_indicator" />