Search code examples
androidandroid-tabhost

How to update specific tab icon in tabhost android?


I've 4 tabs, one of them is notification icon. By default set all icons using tab specifications.

spec = host.newTabSpec("Tab Four");
                        spec.setContent(R.id.tab5);
                        spec.setContent(new Intent(getApplicationContext(), ICNotificationActivity.class));
                            spec.setIndicator("", getResources().getDrawable(R.drawable.tab_notification_noted));
host.addTab(spec);

After a delay i have to update tab 4 icon with another resource file . How to update tabhost? Is there any update funtion available like host.addTab(spec)?


Solution

  • Here i am looping all tabs and change the color and icon of selected tab. in for the loop i am setting all tab to unselected first than only selected tab is set to updated icons and color.

    private void setTabColor(TabHost tabhost, int position) {
        for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
            //      unselected
            tabhost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.tab_unselected);
    
            TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(R.id.tvTabTitle);
            tv.setTextColor(getResources().getColor(R.color.text_white));
        }
        if (position > 0) {
            //      selected
            tabhost.getTabWidget().setCurrentTab(position);
            tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab())
                    .setBackgroundResource(R.drawable.tab_selected);
            TextView tv = (TextView) tabHost.getTabWidget().getChildAt(tabhost.getCurrentTab()).findViewById(R.id.tvTabTitle);
            tv.setTextColor(getResources().getColor(R.color.tab_color));
        }
    
    }
    

    you can call above method by using

        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            //Do something here
         setTabColor(myTabHost, myTabPosition);
        }
    }, 5000);