Tuesday, May 12, 2015

Change TabHost Tab Colors in Android — Set Selected & Unselected Colors

Introduction

In Android you can change the color of TabHost tabs. By default the TabHost appearance is grey. In this example the author changes the unselected tab color to #A45D0A (dark orange / brown tone) and the selected tab color to #A42C0A (dark red).

This article shows how to initialize tabs (each associated with a ListView in the original example), set initial colors, and update colors inside an onTabChanged listener.

Prerequisites

  • A layout that contains a TabHost (for example, with id R.id.TabHost01).
  • A helper method getTabIndicator(Context, String, Drawable) used in the original example to create a tab indicator view — keep your implementation of that helper.

Code example (corrected and formatted)

// Required imports
import android.graphics.Color;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TabHost.OnTabChangeListener;

// ... inside your Activity / Fragment where popupView is available:
final TabHost tabs = (TabHost) popupView.findViewById(R.id.TabHost01);
tabs.setup();

// Optional: show dividers and custom divider drawable
tabs.getTabWidget().setShowDividers(TabWidget.SHOW_DIVIDER_MIDDLE);
tabs.getTabWidget().setDividerDrawable(R.drawable.custom_tab_indicator_divider);

// Create tab 1
TabHost.TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setContent(R.id.listView01);
tab1.setIndicator(getTabIndicator(tabs.getContext(), "Dialed", getResources().getDrawable(R.color.white)));
tabs.addTab(tab1);

// Create tab 2
TabHost.TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setContent(R.id.listView02);
tab2.setIndicator(getTabIndicator(tabs.getContext(), "Missed", getResources().getDrawable(R.color.white)));
tabs.addTab(tab2);

// Create tab 3
TabHost.TabSpec tab3 = tabs.newTabSpec("tab3");
tab3.setContent(R.id.listView03);
tab3.setIndicator(getTabIndicator(tabs.getContext(), "Received", getResources().getDrawable(R.color.white)));
tabs.addTab(tab3);

// Set initial colors: first tab selected color, others unselected
for (int i = 0; i < tabs.getTabWidget().getChildCount(); i++) {
    if (i == 0)
        tabs.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#A42C0A")); // selected
    else
        tabs.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#A45D0A")); // unselected
}

// Handle tab changes
tabs.setOnTabChangedListener(new OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
        // Reset all tabs to unselected color
        for (int i = 0; i < tabs.getTabWidget().getChildCount(); i++) {
            tabs.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#A45D0A")); // unselected
        }

        // Set the currently selected tab color
        tabs.getTabWidget().getChildAt(tabs.getCurrentTab()).setBackgroundColor(Color.parseColor("#A42C0A")); // selected
    }
});

Notes and explanations

  • The original article used #A45D0A for unselected tabs and #A42C0A for the selected tab. These hex values are preserved above.
  • The helper method getTabIndicator(…) used in setIndicator(…) is not included in the original post — keep your implementation or replace with a standard TextView/ImageView as needed.
  • The code corrects several small issues from the original post (case-sensitive method names and inconsistent variable names). For example, Color.parseColor must be used (not parsecolor), and the listener should reference the same TabHost variable (tabs.getCurrentTab()).

Best practices and improvements to consider

  • Use color resources (res/values/colors.xml) instead of hard-coded hex values:
<color name="tab_selected">#A42C0A</color>
<color name="tab_unselected">#A45D0A</color>

Then use ContextCompat.getColor(context, R.color.tabselected) or getResources().getColor(R.color.tabselected, null) on newer APIs.

  • Prefer using a selector drawable for tab backgrounds instead of changing background color in code. A selector makes state handling simpler and moves styling into XML.

  • Consider modern alternatives: TabHost is an older pattern. For new apps, use TabLayout with ViewPager or ViewPager2 (from the Material components / AndroidX) for better integration and styling.

  • Accessibility: ensure tabs have readable text (high contrast) and consider contentDescription when tabs are primarily icon-based.

  • On API 23+ use ContextCompat.getColor(context, … ) or ResourcesCompat to avoid deprecated calls.

Conclusion

Now populate the data for the ListViews associated with each tab. That’s it — Happy Coding..

No comments: