Search code examples
javaandroidxmlandroid-studioandroid-tabhost

How can I change the colors of TabHost?


I want to change the Colors of the Tabs (Activated Tabs and Inactivated Tabs). I know, how I can change the background images, but not, how I can change the colors of the tabs :-(

My Java Code:

public class Inventar extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inventar);

    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
    TabHost.TabSpec spec;
    Intent intent;

    spec = tabHost.newTabSpec("1");
    spec.setIndicator("Inventar 1");
    intent = new Intent(this, Spielregeln.class);
    spec.setContent(intent);
    tabHost.addTab(spec.setIndicator("",getResources().getDrawable(R.drawable.button)));

    spec = tabHost.newTabSpec("2");
    spec.setIndicator("Inventar 2");
    intent = new Intent(this, Login.class);
    spec.setContent(intent);
    tabHost.addTab(spec.setIndicator("",getResources().getDrawable(R.drawable.button)));

    spec = tabHost.newTabSpec("3");
    spec.setIndicator("Inventar 3");
    intent = new Intent(this, Registrieren.class);
    spec.setContent(intent);
    tabHost.addTab(spec.setIndicator("", getResources().getDrawable(R.drawable.tab_spec)));


    tabHost.setCurrentTab(0);
    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabId) {

        }
    });


}

}

My XML Code (tab_spec.xml):

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
        android:drawable="@drawable/inventar" />
    <item
        android:state_selected="false"
        android:drawable="@drawable/freunde" />
</selector>

My XML-Code (main)

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:padding="5dp">

        </FrameLayout>

    </LinearLayout>
</TabHost>
enter code here

Solution

  • You can set using onTabChangedListener,

    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    
            public void onTabChanged(String arg0) {
                for (int i = 0; i < tab.getTabWidget().getChildCount(); i++) {
                    tabHost.getTabWidget().getChildAt(i)
                            .setBackgroundResource(R.drawable.tab_selected); // unselected
                }
                tabHost.getTabWidget().getChildAt(tab.getCurrentTab())
                        .setBackgroundResource(R.drawable.tab_unselected); // selected
    
            }
        });