Nurdin
Nurdin

Reputation: 23883

Android - tabhost not change background color

I already setup my tabhost background colour but it's not working.

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarTabStyle">@style/ActionBarTabStyle</item>
    <item name="actionBarTabStyle">@style/ActionBarTabStyle</item>
</style>

<style name="ActionBarTabStyle"
    parent="@style/Widget.AppCompat.ActionBar.TabView">
    <item name="android:background">@drawable/tab_bg_selector</item>
    <item name="background">@drawable/tab_bg_selector</item>
</style>

tab_bg_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/blueLight" android:state_pressed="true"/>
    <item android:drawable="@color/blueLight" android:state_selected="true"/>
    <item android:drawable="@color/blueDark"/>
</selector>

bottom_tabs.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

Upvotes: 0

Views: 1166

Answers (4)

ebernie
ebernie

Reputation: 820

In your theme, add the following

<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>

Then add MyActionBar style (replace drawable entries with your own):

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <!-- action bar background -->
    <item name="background">@drawable/ab_solid_mytabhost</item>
    <!-- needed for 'stacked' & 'split' action bar (used by tabhost) -->
    <item name="backgroundStacked">@drawable/ab_stacked_solid_mytabhost</item>
    <item name="backgroundSplit">@drawable/ab_bottom_solid_mytabhost</item>
</style>

Your styles.xml should look something like this:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarTabStyle">@style/ActionBarTabStyle</item>
    <item name="actionBarTabStyle">@style/ActionBarTabStyle</item>
</style>

<style name="ActionBarTabStyle" parent="@style/Widget.AppCompat.ActionBar.TabView">
    <item name="android:background">@drawable/tab_bg_selector</item>
    <item name="background">@drawable/tab_bg_selector</item>
    <!-- add these -->
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<!-- add this-->
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <!-- action bar background -->
    <item name="background">@drawable/ab_solid_mytabhost</item>
    <!-- needed for 'stacked' & 'split' action bar (used by tabhost) -->
    <item name="backgroundStacked">@drawable/ab_stacked_solid_mytabhost</item>
    <item name="backgroundSplit">@drawable/ab_bottom_solid_mytabhost</item>
</style>

For a working sample, here's a link to a sample project: https://github.com/ebernie/MyTabHostThemeSample

Upvotes: 0

BSavaliya
BSavaliya

Reputation: 827

You can try it:

tab_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Non focused states -->
    <item android:drawable="@drawable/unselected_tab" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/button_buynow" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
    <!-- Focused states -->
    <item android:drawable="@drawable/unselected_tab" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/selected_tab" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
    <!-- Pressed -->
    <!-- Non focused states -->
    <item android:drawable="@drawable/unselected_tab" android:state_focused="false" android:state_pressed="true" android:state_selected="false"/>
    <item android:drawable="@drawable/button_buynow" android:state_focused="false" android:state_pressed="true" android:state_selected="true"/>
    <!-- Focused states -->
    <item android:drawable="@drawable/unselected_tab" android:state_focused="true" android:state_pressed="true" android:state_selected="false"/>
    <item android:drawable="@drawable/button_buynow" android:state_focused="true" android:state_pressed="true" android:state_selected="true"/>

</selector>

After declaration in you MainActivity, you can use following code:

TabWidget widget = th.getTabWidget();

    for (int i = 0; i < widget.getChildCount(); i++) {
        View v = widget.getChildAt(i);
        v.setBackgroundResource(R.drawable.tab_selector);
    }

Upvotes: 0

Waqar Khan
Waqar Khan

Reputation: 478

If you are OK to change color in code than you can try this:

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

        tab.getTabWidget().getChildAt(tab.getCurrentTab())
            .setBackgroundResource(R.drawable.tab_unselected); // selected
    }
});

Upvotes: 0

shkschneider
shkschneider

Reputation: 18243

Since you are using AppCompat library, use this:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarTabStyle">@style/ActionBarTabStyle</item>
    <item name="actionBarTabStyle">@style/ActionBarTabStyle</item>
</style>

<style name="ActionBarTabStyle"
       parent="@style/Widget.AppCompat.ActionBar.TabView">
    <item name="android:background">@drawable/tab_bg_selector</item>
    <item name="background">@drawable/tab_bg_selector</item>
</style>

You needed the Widget.AppCompat.ActionBar.TabView parent and the android: variants for AppCompat support.

That is according to ActionBar's documentation and ActionBar Tab's documentation.

Upvotes: 0

Related Questions