Change tabs text color in TabLayout to different colors programmatically
set tab text color this way :
tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.color.tab_selector));
tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.indicator));
Try this and let me know if this works for you:
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition() == 0) {
tabLayout.getTabAt(0).getIcon().setAlpha(255);
tabLayout.getTabAt(1).getIcon().setAlpha(100);
tabLayout.getTabAt(2).getIcon().setAlpha(100);
} else if (tab.getPosition() == 1) {
tabLayout.getTabAt(0).getIcon().setAlpha(100);
tabLayout.getTabAt(1).getIcon().setAlpha(255);
tabLayout.getTabAt(2).getIcon().setAlpha(100);
} else if (tab.getPosition() == 2) {
tabLayout.getTabAt(0).getIcon().setAlpha(100);
tabLayout.getTabAt(1).getIcon().setAlpha(100);
tabLayout.getTabAt(2).getIcon().setAlpha(255);
}
}
@Surya Prakash Kushawah your way is better.
Create a style in style.xml and call in xml layout like below
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">@color/colorAccent</item>
<item name="tabIndicatorHeight">2dp</item>
<item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
<item name="tabSelectedTextColor">@color/colorAccent</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">@dimen/title_text_size</item>
<item name="android:textColor">@color/secondaryText</item>
<item name="textAllCaps">false</item>
<item name="android:textStyle">normal</item>
</style>
Call here
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="@android:color/white"
app:tabMode="scrollable"
**style="@style/MyCustomTabLayout"**
app:tabGravity="fill" />