Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 968
Mercy Avatar asked Feb 20 '12 05:02

Mercy


People also ask

How do you change the text color on a selected tab?

Format the color of a worksheet tabRight-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.

How do I change the color of icon of the selected tab of tabLayout?

One possible solution is apparently with selectors. But in that case, I would have to find both a white and a gray version of the icon and then switch the icon when the tab becomes selected or deselected.


2 Answers

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
}
like image 132
Hiral Vadodaria Avatar answered Oct 06 '22 20:10

Hiral Vadodaria


You can set Listener for your TabHost using setOnTabChangedListener and change it dynamically,

  public void onCreate(Bundle savedInstanceState){
   // add your tabs here

   // set the First Tab as selected Tab.
  setSelectedTabColor();
}

Create a method that will set the Selected and Unselected color of Tab.

 private void setSelectedTabColor() {
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)  
        {  
            tabHost.getTabWidget().getChildAt(i)
                                            .setBackgroundColor(Color.WHITE);  
        }  
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                                              .setBackgroundColor(Color.RED); 
    }

Then inside your onTabChanged() you can dynamically change the Background.

@Override  
    public void onTabChanged(String tabId) {  
        setSelectedTabColor(); 
    } 

You can use the same for selected and unselected Tab, here is the Blog for the same.

like image 44
Lalit Poptani Avatar answered Oct 06 '22 21:10

Lalit Poptani