Mercy Mercy - 5 months ago 33
Android Question

Is it possible to change the color of selected Tab in android?

Hi i have two tabs in my tab widget,i want to apply the two different color for two tabs.am searching everywhere,mostly all colors are same while applying the tab.

update

first tab when selected red color

second tab when selected blue color

Here my code

tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");//these are color red
TabSpec secondTabSpec = tabHost.newTabSpec("tid1");//these color blue
firstTabSpec.setIndicator("Sales Info",getResources().getDrawable(R.drawable.sales));
Intent photosIntent = new Intent(this, a.class);
firstTabSpec.setContent(photosIntent);
secondTabSpec.setIndicator("Service Info",getResources().getDrawable(R.drawable.services));
Intent photosIntent1 = new Intent(this, b.class);
secondTabSpec.setContent(photosIntent1);
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);

Answer

Try this:

...onCreate(){

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

    @Override
    public void onTabChanged(String arg0) {

        setTabColor(tabHost);
    }
     });
     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_CYAN); //unselected

    if(tabhost.getCurrentTab()==0)
           tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(COLOR_RED); //1st tab selected
    else
           tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(COLOR_BLUE); //2nd tab selected
}