Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android tabhost change text color style

Trying to change tabhost text color, in this code I can change tabhost background color(not text color)

tabHost.setOnTabChangedListener(new OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabId) {
          for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            tabHost.getTabWidget().getChildAt(i)
                            .setBackgroundColor(Color.parseColor("#FF0000")); // unselected
          }

          tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                        .setBackgroundColor(Color.parseColor("#0000FF")); // selected

        }
});

how can I change tabhost text color?

like image 902
user3345767 Avatar asked Mar 20 '14 12:03

user3345767


2 Answers

You may change color of Tabhost text as follow.

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

    @Override
    public void onTabChanged(String tabId) {

        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); // unselected
            TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
            tv.setTextColor(Color.parseColor("#ffffff"));
        }

        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
        TextView tv = (TextView) tabhost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
        tv.setTextColor(Color.parseColor("#000000"))

    }
});

EDIT:

To set text color initially in your activity, you can use this code in onResume() function

TabHost tabhost = getTabHost();
    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("#000000"));
    } 
like image 169
Mukesh Kumar Singh Avatar answered Sep 28 '22 01:09

Mukesh Kumar Singh


This can actually be done using XML themes. The TabWidget uses android:textColorPrimary for the selected tab and android:textColorSecondary for the unselected ones. Thus, you can achieve a text color change like this:

In styles.xml:

<style name="TabWidgetTheme" parent="AppTheme">
    <item name="android:textColorPrimary">@color/your_primary_color</item>
    <item name="android:textColorSecondary">@color/your_secondary_color</item>
</style>

In your layout:

<TabHost
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:theme="@style/TabWidgetTheme"/>

Note that the android:theme should not be directly in the TabWidget itself, but rather the containing TabHost or similar.

like image 20
Will Molter Avatar answered Sep 28 '22 00:09

Will Molter