1


I'm new to Android and I need your help!
I love default tabWidget style, but I need white color in tab title.
This is what I have for now tabwidget
I want "TAB1" and "TAB2" in white color.
I tried my own tabwidget style. Here's the code

<!-- styles.xml -->

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:tabWidgetStyle">@style/LightTabWidget</item>
</style>
<style name="LightTabWidget" parent="@android:style/Widget.TabWidget">
    <item name="android:textColor">@color/tabTitle</item>
</style>

This will change title text color but it change all tab style enter image description here

How I can change text color only? Without changing the rest?
Thanks for the help!

UPDATE I'm using tabHost. Is it possible to do what I need with it not with TabLayout?

Ossir
  • 493
  • 1
  • 8
  • 19

2 Answers2

3

Inside your onCreate or onCreateView try this it worked for me!

TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tab_bhishilayout);
tabLayout.addTab(tabLayout.newTab().setText("OPEN"));
tabLayout.setSelectedTabIndicatorColor(Color.RED);//set tab indicator color
tabLayout.setTabTextColors(ColorStateList.valueOf(Color.BLACK));//set tab text color

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"))

    }
});
1

put these lines in your xml-

app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@android:color/black"

<android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed"
        app:tabTextAppearance="@style/MyTabLayoutTextAppearance"
        app:tabIndicatorColor="@color/black"
        app:tabSelectedTextColor="@android:color/white"
        app:tabTextColor="@android:color/black"/>
sumit singh
  • 588
  • 1
  • 5
  • 20