Change the tab indicator color on Android

2

I have tried in many ways to change the color of what they call a "tab indicator", but without success ... Even using the "Android Action Bar Style Generator" the color of this indicator remains light blue.

test http://nsae01.casimages.net/img/2014/09/11/ 140911033853988172.png

I'm using versions 3.0+ (with AppCompat). My XML is exactly as the generator passed to me ...

Generated Layout

    
asked by anonymous 11.09.2014 / 15:45

1 answer

2

The FragmentTabHost returns the TabWidget (only one), which is the View responsible for drawing those tabs. Every child of him is a different flap.

The color setting of Tab of Style Generator applies only to Tab of ActionBar .

In your case you are using TabHost and TabWidget , and this style does not apply to them unfortunately.

To customize the tab, you need to retrieve TabWidget and stylize each child. This way:

// Assim como voce mencionou
TabWidget tabWidget = mTabHost.getTabWidget();

// Como o TabWidget eh um ViewGroup, ele tem filhos e podemos iterar
// sobre os mesmos
int childCount = tabWidget.getChildCount();

for(int i = 0; i < childCount; ++i) {
    View child = tabWidget.getChildTabViewAt(i);
    // Que eh o mesmo que
    //View child = tabWidget.getChildAt(i);
    // Vide o codigo fonte

    // O Drawable vai variar conforme o nome do seu tema, confirme se tem
    // algum nome parecido com esse e altere aqui
    child.setBackgroundResource(R.drawable.tab_indicator_ab);
}

Use this code at the end of the TabHost setting.

The file tab_indicator_ab.xml that is in the /res/drawable directory is very similar to this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_ab" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_ab" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_ab" />

    <!-- Pressed -->
    <!--    Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_ab" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_ab" />

    <!--    Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_ab" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_ab" />
</selector>
    
11.09.2014 / 16:36