2

I am trying to change the background color of tab in TabActivity. for that I did like below,

tabHost.getTabWidget().getChildAt(totalTabs1-1).setBackgroundColor(Color.parseColor("#984b9d"));

but its not working properly what i want.

Is there any other way to do it?

Thank You

Narendra
  • 1,868
  • 6
  • 25
  • 39

3 Answers3

2

For this u have to write a xml file for tab selector inside drawable folder.

tab_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/tab_selectinfo"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/tab_unselectinfo" />
</selector>

and at the time of initialization of tab just do like below,

tabHost.newTabSpec("Info").setIndicator("Info", res.getDrawable(R.drawable.tab_selector)).setContent(intent);
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Jyosna
  • 4,436
  • 13
  • 60
  • 93
1

You can try this:

...
setTabColor(tabHost);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {

    @Override
    public void onTabChanged(String arg0) {

        setTabColor(tabHost);
    }
});    
...
//Change The Backgournd Color of Tabs
public void setTabColor(TabHost tabhost) {      

     for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)  
            tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.DKGRAY); //unselecte
     tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.LTGRAY); // selected
}
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
0

use following

for (int i = 0; i < Global.host.getTabWidget().getChildCount(); i++) {
            Global.host.getTabWidget().getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.inactbg));
            TextView tv = (TextView) Global.host.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            tv.setTextColor(Color.parseColor("#ffffff"));

        }
bindal
  • 1,940
  • 1
  • 20
  • 29