How to change selected Tab Text color using TabLayout from code in Android?

rastha67 picture rastha67 · May 3, 2016 · Viewed 79.4k times · Source

enter image description here

I'm using android.support.widget.TabLayout to make a Tab view, and I want to change the selected tabs text color from code (not from xml or by styling). How can I do this ?

Answer

Shaan_B picture Shaan_B · May 3, 2016

If you are using the design support library add this code to your tab activity.

tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FF0000"));
tabLayout.setSelectedTabIndicatorHeight((int) (5 * getResources().getDisplayMetrics().density));
tabLayout.setTabTextColors(Color.parseColor("#727272"), Color.parseColor("#ffffff"));

This will set the tab text color as well as tab indicator color in your tab activity.