0
  1. I have added custom layout.
  2. I had used tabLayout.setOnTabSelectedListener(); but still, it's not showing selected icon change.
  3. I had done this
  4. I want to do thisenter image description here
Akshay Katariya
  • 1,464
  • 9
  • 20

2 Answers2

0
tabLayout.setOnTabSelectedListener(
            new TabLayout.ViewPagerOnTabSelectedListener(viewPager) 
            {

                @Override
                public void onTabSelected(TabLayout.Tab tab) 
                {
                    super.onTabSelected(tab);
                    //Set your color here
                    int tabIconColor = ContextCompat.getColor(context, R.color.colorActive);
                    tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) 
                {
                    super.onTabUnselected(tab);
                    //Set your color here
                    int tabIconColor = ContextCompat.getColor(context, R.color.colorInactive);
                    tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) 
                {
                    super.onTabReselected(tab);
                }
            }
    );
Akshay Katariya
  • 1,464
  • 9
  • 20
  • I used this code, but on default position = 0 doesn't work. Can you have solution for that? @Akshay – Parth Mistry Feb 20 '18 at 07:28
  • "default position = 0 doesn't work " means it does not work every time or under specific condition like first time it's not working and then it works – Akshay Katariya Feb 20 '18 at 07:39
0

In your xml use style like this

  <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        style="@style/AppTabLayout"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="?attr/colorPrimary"
        />

and in your Style.xml use tabSelectedTextColor with your value like this .

<style name="AppTabLayout" parent="Widget.Design.TabLayout">
        <item name="tabIndicatorHeight">0dp</item>
        <item name="tabPaddingStart">0dp</item>
        <item name="tabPaddingEnd">0dp</item>
        <item name="tabBackground">?attr/selectableItemBackground</item>
        <item name="tabTextAppearance">@style/AppTabTextAppearance</item>
        <item name="tabSelectedTextColor">#ffffff</item> //your color 
        <item name="tabGravity">fill</item>
        <item name="tabMode">fixed</item>

    </style>

and its done .:)

Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51