Narendra
Narendra

Reputation: 1868

change tab background color in TabActivity

I am trying to change the background color of tab in TabActivity. for that I did like below,

tabHost.getTabWidget().getChildAt(totalTabs1-1).setBackgroundColor(Color.parseColor("#984b9d"));

but its not working properly what i want.

Is there any other way to do it?

Thank You

Upvotes: 2

Views: 1280

Answers (3)

Jyosna
Jyosna

Reputation: 4446

For this u have to write a xml file for tab selector inside drawable folder.

tab_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/tab_selectinfo"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/tab_unselectinfo" />
</selector>

and at the time of initialization of tab just do like below,

tabHost.newTabSpec("Info").setIndicator("Info", res.getDrawable(R.drawable.tab_selector)).setContent(intent);

Upvotes: 2

bindal
bindal

Reputation: 1932

use following

for (int i = 0; i < Global.host.getTabWidget().getChildCount(); i++) {
            Global.host.getTabWidget().getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.inactbg));
            TextView tv = (TextView) Global.host.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            tv.setTextColor(Color.parseColor("#ffffff"));

        }

Upvotes: 0

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

You can try this:

...
setTabColor(tabHost);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {

    @Override
    public void onTabChanged(String arg0) {

        setTabColor(tabHost);
    }
});    
...
//Change The Backgournd Color of Tabs
public void setTabColor(TabHost tabhost) {      

     for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)  
            tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.DKGRAY); //unselecte
     tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.LTGRAY); // selected
}

Upvotes: 1

Related Questions