Search code examples
androidtabsandroid-tabhostbackground-color

Change Tab color depending on which tab is selected


I have an app with a MainActivity that extends TabActivity (i know it's deprecated but too much to do to change whole app).

So in my app I use tabhost to create 3 tabs like this:

TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");

firstTabSpec.setIndicator("tab1").setContent(
            new Intent(this, tab1.class));
    secondTabSpec.setIndicator("tab2").setContent(
            new Intent(this, tab2.class));
    thirdTabSpec.setIndicator("tab3").setContent(
            new Intent(this, tab3.class));

    /* Add tabSpec to the TabHost to display. */

    tabHost.addTab(firstTabSpec);
    tabHost.addTab(secondTabSpec);
    tabHost.addTab(thirdTabSpec);


    //Changing the tabs text color on the tabs
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
    { 
        TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); 
        tv.setTextColor(Color.parseColor("#ffffff"));
    } 

    // remove divider
    tabHost.getTabWidget().setDividerDrawable(null);

    tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#90a4ae"));
    tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#607d8b"));
    tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#607d8b"));

So my code creates the 3 tabs that link to 3 different activities and sets the color of the tabs. First tab has at first load a different color than the other two.

I want the color of the tabs to change depending on which one is selected. So when I press the second tab i want the first to get #607d8b color and the second to get #90a4ae. Same for the third one.

Tried to implement a OnTabChangeListener but couldn't get it to work. Tried to to this:

    tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#90a4ae"));
    tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#607d8b"));
    tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#607d8b"));

with changed colors inside each loaded tab activity but I get error that it can't resolve tabhost (as it should, since it's defined in MainActivity.


Solution

  • tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    
        @Override
        public void onTabChanged(String tabId) {
            // TODO Auto-generated method stub
            for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
                    tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#54C4C6")); // unselected
            }
    
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#114C5A")); // selected
        }
    });