Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Tab Text color [duplicate]

Possible Duplicate:
Android: Change Tab Text Color Programmatically

How can we change the text color in android tab.

like image 791
Altaf Avatar asked Mar 01 '11 13:03

Altaf


People also ask

How do I change the color of my tab text?

Right-click the worksheet tab whose color you want to change. Choose Tab Color, and then select the color you want. The color of the tab changes, but not the color of the font. When you choose a dark tab color, the font switches to white, and when you choose a light color for the tab, the font switches to black.

How do you color text on Android?

Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.

What is TabLayout?

TabLayout is used to implement horizontal tabs. TabLayout is released by Android after the deprecation of ActionBar. TabListener (API level 21). TabLayout is introduced in design support library to implement tabs. Tabs are created using newTab() method of TabLayout class.


2 Answers

I use the ColorStateList, find it more elegant. Here is an example :

tab_text.xml :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@color/tab_active" />
    <item android:state_selected="false" android:color="@color/tab_inactive" />
</selector>

In your TextView, just set the textColor to point to this file with :

android:textColor="@color/tab_text"
like image 140
isneesh Avatar answered Sep 25 '22 09:09

isneesh


You can use following code

    TabHost tabHost = getTabHost();
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
        { 
            TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
            tv.setTextColor(Color.parseColor("#ffffff"));
        } 
        TextView tv = (TextView) tabhost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
        tv.setTextColor(Color.parseColor("#000000"))
like image 21
Tushar Vengurlekar Avatar answered Sep 25 '22 09:09

Tushar Vengurlekar