2

I'm trying to change the TabWidget text color, without success, even though I've tried different way to change it (see code below.)

My background tabs is an image:

for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
    tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT); 
}

I do not know if this creates some sort of conflict with what I want to do now.

Solution1:

main.xml

....
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/tabbarbackground"
        android:tabStripEnabled="false"            
        style="@style/TabText"
        /> ....

style.xml

... <style name="TabText">
    <item name="android:textColor">@color/tab_text_color</item> </style> ....

tab_text_color.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:textColor="#2daed9" />
    <item android:state_selected="false" android:color="#FFFFFF" />
</selector>

solution 2

for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
    tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);         
    RelativeLayout rl = (RelativeLayout) tabHost.getTabWidget().getChildAt(i);
    TextView textView = (TextView) rl.getChildAt(1);
    textView.setTextColor(R.color.tab_text_color);
}

tab_text_color.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:textColor="#2daed9" />
    <item android:state_selected="false" android:color="#FFFFFF" /> </selector>

but neither solution works.

However, if I change the second solution

textView.setTextColor (R.color.tab_text_color);

to

textView.setTextColor (Color.parseColor ("# ....")); 

It works, except this solution does not change the color of text when I click on it.

Thanks.

Chilledrat
  • 2,593
  • 3
  • 28
  • 38
romruben
  • 194
  • 3
  • 17
  • see here for better solution of how to change the text color. http://stackoverflow.com/questions/9982182/using-selector-to-change-textview-text-color/15498013#15498013! – Sagar Shah Mar 19 '13 at 11:28

3 Answers3

6

I was able to solve, the solution isn't elegant but works. I hope who is usefull for somebody:

First, I must set init color for the textview of all tabs:

for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
    vg = (ViewGroup) getTabHost().getTabWidget().getChildAt(i);
    tv = (TextView) vg.getChildAt(1);
    tv.setTypeface(font);
    if (i == 0) {
        tv.setTextColor(Color.parseColor("#2daed9"));
        Currentab = 0;
    } else {
        tv.setTextColor(R.color.GrisOscuro);
    }
}

And then, I set in the override method ontabchanged, the change color for each tab. The tab pulsed are i (getTabHost().getCurrentTab()). And the last tab who I press is Currentab.

getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
    public void onTabChanged(String tabId) {
        int i = getTabHost().getCurrentTab();
        if (Currentab != i) {
            vg = (ViewGroup) getTabHost().getTabWidget()
                    .getChildAt(Currentab);
            tv = (TextView) vg.getChildAt(1);
            tv.setTextColor(R.color.GrisOscuro);

            Currentab = i;
            vg = (ViewGroup) getTabHost().getTabWidget()
                    .getChildAt(i);
            tv = (TextView) vg.getChildAt(1);
            tv.setTextColor(Color.parseColor("#2daed9"));
        }
    }
});

Sorry for my english, I hope is useful for somebody =) Bye! ;D

romruben
  • 194
  • 3
  • 17
4

In your solution 2:

TabWidget tabwidget=mTabHost.getTabWidget();
for(int i=0;i<tabwidget.getChildCount();i++){
    TextView tv=(TextView) tabwidget.getChildAt(i).findViewById(android.R.id.title);
    tv.setTextColor(this.getResources().getColorStateList(R.color.tab_text_xml));
}
cameron
  • 2,976
  • 3
  • 23
  • 35
1

try to write this methode:

public void onTabChanged(String tabId) {

for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
TextView tv = (TextView) tabhost.getTabWidget()
.getChildAt(i).findViewById(R.id.your_text_id);
    tv.setTextColor(#FFFFFF);

}

TextView tv = (TextView)    tabHost.getTabWidget().
getChildAt(tabHost.getCurrentTab()).findViewById(R.id.your_text_id);

tv.setTextColor(#2daed9);
}     
Xenione
  • 2,174
  • 1
  • 23
  • 30
  • thanks but won't worked for my, because I use a tabwidget without textview and imageview on xml... I try do something similar but I can't because i cant access an concrete tab only if I do with a for: for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) { RelativeLayout rl = (RelativeLayout) tabHost.getTabWidget().getChildAt(i); TextView textView = (TextView) rl.getChildAt(1);// textView.setTextColor(Color.parseColor("#FFFFFF")); } – romruben Jun 25 '12 at 16:48