1

I'm using TabHost in my code and I am wondering how I can change the text colour? I thought it would be something to do with the XML but upon looking at it I don't think it is:

public class HealthyEating extends TabActivity{
Resources res;
TabHost tabHost;
Intent intent;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_healthy_eating);

    res = getResources();
    tabHost = getTabHost();
    TabHost.TabSpec spec;

    intent = new Intent().setClass(this, BreakfastRecipe.class);
    spec = tabHost.newTabSpec("Breakfast Recipes").setIndicator("Breakfast Recipes")
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, LunchRecipe.class);
    spec = tabHost.newTabSpec("Lunch Recipes").setIndicator("Lunch Recipes")
            .setContent(intent);
    tabHost.addTab(spec);
    tabHost.setCurrentTab(0);


}

2 Answers2

0

You can change it through the tabs title TextView. For details see:

Android: Change Tab Text Color Programmatically

Example usage for you having a text color of red would be:

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.myproject.R;

public class HealthyEating extends TabActivity {
    Resources res;
    TabHost tabHost;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_healthy_eating);

        res = getResources();
        tabHost = getTabHost();
        TabHost.TabSpec spec;

        intent = new Intent().setClass(this, BreakfastRecipe.class);
        spec = tabHost.newTabSpec("Breakfast Recipes").setIndicator("Breakfast Recipes")
                .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, LunchRecipe.class);
        spec = tabHost.newTabSpec("Lunch Recipes").setIndicator("Lunch Recipes")
                .setContent(intent);
        tabHost.addTab(spec);
        tabHost.setCurrentTab(0);

        int titleColor = Color.RED; //<-- change this to the color you want the title text to be
        for(int i = 0;i < tabHost.getTabWidget().getChildCount(); i++)
        {
            TextView textView = (TextView)tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            textView.setTextColor(titleColor);
        }
    }
}

The for loop goes through each of the tabs that you've added to the TabHost and accesses the TextView that is associated with the title label. The setTextColor method is used to change the text color to what you want (in this example red). It may be worthwhile checking out the docs for TabWidget.

Note in particular the extra import statements that you will need: import android.widget.TextView and import android.graphics.Color

This example worked for me with the following activity_healthy_eating file:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabhost">

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:orientation="vertical"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_marginBottom="5dip">
        </TabWidget>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
        </FrameLayout>
    </LinearLayout>
</TabHost>

If you're getting any errors please post them and I'll try to address them.

More information on Color (docs) in Android can be found here

An online tool for finding the hex color code you're after can found here.

Community
  • 1
  • 1
peterrhodesdev
  • 241
  • 4
  • 14
  • Pretty new to java, I tried to implement that and it really didn't make much sense to me I'm afraid :/ – James Wolfe Mar 03 '15 at 15:10
  • You should be able to copy/paste from the code in my edited answer. If that doesn't work, let me know what the issue is and I'll try to help. – peterrhodesdev Mar 03 '15 at 16:11
0
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"));
} 
altan yuksel
  • 173
  • 1
  • 6