Luke Batley
Luke Batley

Reputation: 2402

Android change background of tabs

Does anyone know how i can edit a tabs background so i have a red gradient background when its not selected and when it is selected a dark red gradient? also change the text color to white?

Upvotes: 2

Views: 1891

Answers (2)

Shrikant
Shrikant

Reputation: 1580

You can use this code

TabHost.TabSpec spec;
TabHost tabHost = getTabHost();
spec = tabHost.newTabSpec("1").setIndicator("Tab Host 1", res.getDrawable(R.drawable.XXX)).setContent(intent_name);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
setTabColor(tabHost);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
          setTabColor(tabHost);
    }
});
}

public static void setTabColor(TabHost tabhost) {
    for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#000000")); // unselected
    }

    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#74df00")); // selected
}

Upvotes: 3

DirkV
DirkV

Reputation: 353

Create a new file in your drawables folder, background.xml for instance.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use red-->
    <item android:drawable="@drawable/gradient_red"
          android:state_selected="true"/>
    <!-- When not selected, use dark rebg-->
    <item android:drawable="@drawable/gradien_dark_red"/>
</selector>

Use the new drawable for your background.

Upvotes: 0

Related Questions