Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 284
rastha67 Avatar asked May 03 '16 09:05

rastha67


People also ask

How do you change the typeface of a selected tab of a tabLayout?

You can change the font family for selected/unselected tab only programmatically. You can listen which Tab is selected and unselected with TabLayout. OnTabSelectedListener and on onTabSelected(TabLayout. Tab tab) callback you can change the Typeface for the selected Tab and on onTabUnselected(TabLayout.

How do I use tabLayout on Android?

This example demonstrates how do I create a Tab Layout in android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 3 − Add the following code to res/layout/activity_main. xml.


2 Answers

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" /> 
like image 94
Mohammedsalim Shivani Avatar answered Sep 20 '22 16:09

Mohammedsalim Shivani


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.

like image 39
Shaan_B Avatar answered Sep 18 '22 16:09

Shaan_B