Search code examples
javaandroidandroid-tabhost

Change tab layout tab color?


Im Creating an android studio app , i want when user clicked on a tab button the tab icon/color

change this is my code :

       tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {

               int id= tabHost.getCurrentTab();
              //  TabLayout.setSelectedTabIndicatorColor(int color); // Error because of non static method

//                if(id==0){
//                    TabHost.TabSpec tab1spec = tabHost.newTabSpec("List");
//                    tab1spec.setIndicator("", getResources().getDrawable(R.drawable.home2));
//                }
//                if(id==1){
//                    TabHost.TabSpec tab1spec = tabHost.newTabSpec("Config");
//                    tab1spec.setIndicator("", getResources().getDrawable(R.drawable.lab2));
//                }
//                if(id==2){
//                    TabHost.TabSpec tab1spec = tabHost.newTabSpec("More");
//                    tab1spec.setIndicator("", getResources().getDrawable(R.drawable.more2));
//                }

            }
        });

Any ideas ?

Thanks


Solution

  •   tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
                  @Override 
                  public void onTabChanged(String tabId) {
                    int tab = tabHost.getCurrentTab();
                    tabHost.getTabWidget().getChildAt(tab).setBackgroundColor(Color.parseColor("#FFFFFF"));
                    setTabColor(tabHost);
                  } 
                });        
        } 
    
        public static void setTabColor(TabHost tabhost) {
            for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
            { 
                tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE); //unselected
            } 
            tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#FFFFFF")); // selected
    }