How to change selected Tab Text color using TabLayout from code in Android?
It's so simple using XML. Just add the following 2 attributes in your tab layout.
app:tabSelectedTextColor="@color/color_primary_text"app:tabTextColor="@color/color_secondary_text"
So, your code would look something like this.
<android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentStart="true" android:layout_gravity="bottom" android:background="@color/button_background" android:fillViewport="true" app:tabBackground="@drawable/fixed_bottom_button" app:tabIndicatorColor="@color/color_primary_text" app:tabMode="fixed" app:tabSelectedTextColor="@color/color_primary_text" app:tabTextColor="@color/color_secondary_text" />
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.
Please check out following answer
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { View view = tab.getCustomView(); RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.layout_background); relativeLayout.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.white)); TypefacedTextView selectedText = (TypefacedTextView) view.findViewById(R.id.txt_tab_name); selectedText.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary)); } @Override public void onTabUnselected(TabLayout.Tab tab) { View view = tab.getCustomView(); RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.layout_background); relativeLayout.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary)); TypefacedTextView selectedText = (TypefacedTextView) view.findViewById(R.id.txt_tab_name); selectedText.setTextColor(ContextCompat.getColor(getActivity(), R.color.white)); } @Override public void onTabReselected(TabLayout.Tab tab) { } });
where tabLayout is object of TabLayout Class