Sunday, April 28, 2013

How to Change Background Color of Tab Control in android?

In Android, you can change Tab Control's background color. You can specify different color for selected and non selected tabs.

To set default color for all tabs of tab view and make first child selected, write the following code in "onCreate" method of activity class,


for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(color.Blue);
}
tabHost.getTabWidget().setCurrentTab(1);
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.White);

To change the tab color on selection write the following code in "onTabChange" method.

@Override
public void onTabChanged(String tabId) {


for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(color.Blue));
}

tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(color.White);

}

No comments:

Post a Comment