Search code examples
androidfragment-tab-host

Unable to customize the tabs of fragment tab host


Im trying to create a tabs using android.support.v4.app.FragmentTabHost.

Im trying to change the background colour of tab to white and text colour of tabs to black but it doesn't work.

Following is my code.

<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:textColor="@color/trans_white"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

Solution

  • try this way to customize your tabhost
    tab_selector.xml

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

    XML

    <?xml version="1.0" encoding="utf8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/host"
        android:layout_width="fillparent"
        android:layout_height="fillparent" >
    
        <RelativeLayout
            android:layout_width="fillparent"
            android:layout_height="fillparent"
            android:orientation="vertical"
            android:padding="6dp" >
    
            <TabWidget
                android:id="@android:id/tab"
                android:layout_width="fillparent"
                android:layout_height="wrapcontent" />
    
            <FrameLayout
                android:layout_below="@android:id/tab"
                android:id="@android:id/tabattributes"
                android:layout_width="fillparent"
                android:layout_height="fillparent"
                android:padding="6dp" />
        </RelativeLayout>
    
    </TabHost>
    

    Java Code

    public class TabCustomizationActivity extends TabActivity implements
            OnTabChangeListener {
        /** Called when the activity is first created. */
        TabHost tHost;
    
        @Override
        public void onCreate(savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Resources resources = getResources();
    
            tHost = getTabHost();
            TabHost.TabSpec tSpec;
            Intent intent;
    
            intent = new Intent().setClass(this, FirstActivity.class);
    
            tSpec = tHost.newTabSpec("first").setIndicator("One")
                    .setContent(intent);
            tHost.addTab(tSpec);
    
            intent = new Intent().setClass(this, FirstActivity.class);
            tSpec = tHost.newTabSpec("second").setIndicator("Second")
                    .setContent(intent);
            tHost.addTab(tSpec);
    
            intent = new Intent().setClass(this, FirstActivity.class);
            tSpec = tHost.newTabSpec("third").setIndicator("Third")
                    .setContent(intent);
            tHost.addTab(tSpec);
    
            tHost.setCurrentTab(0); // Default Selected Tab
    
            tHost.setOnTabChangedListener(this);
    
            tHost.getTabWidget().getChildAt(0).getLayoutParams().height = 40;
            tHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.WHITE);
            tHost.getTabWidget().getChildAt(1).getLayoutParams().height = 40;
            tHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.WHITE);
            tHost.getTabWidget().getChildAt(2).getLayoutParams().height = 40;
            tHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.WHITE);
    
            tHost.getTabWidget().getChildAt(0)
                    .setBackgroundColor(Color.rgb(00, 219, 239));
    
        }
    
        @Override
        public void onTab(String tabId) {
            if (tabId.equals("first")) {
                tHost.getTabWidget().getChildAt(0)
                        .setBackgroundResource(R.drawable.tab_selector);
                tHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.WHITE);
                tHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.WHITE);
            } else if (tabId.equals("second")) {
                tHost.getTabWidget().getChildAt(1)
                        .setBackgroundResource(R.drawable.tab_selector);
    
                tHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.WHITE);
                tHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.WHITE);
            } else if (tabId.equals("third")) {
                tHost.getTabWidget().getChildAt(2)
                        .setBackgroundResource(R.drawable.tab_selector);
                tHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.WHITE);
                tHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.WHITE);
            }
    
        }
    
    }
    

    For more see this

    enter image description here