Search code examples
androidfragment-tab-host

How to change default tabwidget color on loading in android?


Can anyone suggest me how to set background color for tab widget. i tried with some codes and with examples in the net but still i can't able to change the color. Advance thanks guys. Below is my code:

mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("My Families", getResources().getDrawable(R.drawable.tabselect)),
                        Main.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("My Pictures", getResources().getDrawable(R.drawable.tabunselect)),
                    Second.class, null);

below in my xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#2B78E4" />
</shape>
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#999999" />
</shape>

Solution

  • You can write the following code in for (int i = 0; i < tab.getTabWidget().getChildCount(); i++) tab.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_selected); tab.getTabWidget().getChildAt(tab.getCurrentTab()).setBackgroundResource(R.drawable.tab_unselected);

    in onCreate() method