1

I'm using the new material components I want to achieve this design enter image description here

I found these answers for the old TabLayout

How do I change the color of icon of the selected tab of TabLayout?

Changing the background color of a Tab in TabLayout (Android design support library) doesn't occupy the entire tab space

I think the new TabLayout has an easier way to set the background of the tab and handle the text and Icon color changes

humazed
  • 74,687
  • 32
  • 99
  • 138

1 Answers1

12

Here are the steps to set the Tab background color:

first the code and then some brief explanation:

The code:

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    app:tabBackground="@drawable/tab_background_color_selector"
    app:tabIconTint="@color/tab_content_color_selector"
    app:tabIndicator="@null"
    app:tabSelectedTextColor="@color/md_white_1000"
    app:tabTextColor="@color/secondaryColor">

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:icon="@drawable/ic_list"
        android:text="list" />

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:icon="@drawable/ic_calender"
        android:text="calender" />
</com.google.android.material.tabs.TabLayout>

drawable/tab_background_color_selector.xml

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

color/tab_content_color_selector.xml

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

The Explanation

tabBackground: is the tab background and should be a drawable selector.

tabIconTint: is for icon color and it must me color selector.

tabIndicator="@null" : as it is useless in this case.

tabSelectedTextColor, tabTextColor are self explanatory.

Community
  • 1
  • 1
humazed
  • 74,687
  • 32
  • 99
  • 138