Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I style a TabWidget in Ice Cream Sandwich?

I'm using the depreciated libraries for TabHost/TabWidget in an Ice Cream Sandwich app. I don't have the time available to me to become familiar with ActionBarSherlock, so I've had to go into the app the only way I knew how.

I'd like to know how I can go about styling the TabWidget and its Tab objects, from changing the selected tab colour, to the background colours and image? Using standard styling and themeing doesn't seem to work.

like image 729
Andrew Weir Avatar asked Apr 11 '12 08:04

Andrew Weir


1 Answers

Assuming the deprecated library works as it always has, as it should, this is the procedure I have used to colour my tabs. I just set the background in code as follows, as it wasn't direcly accessible in xml:

 TabWidget tabs = (TabWidget)getTabWidget();            
        for (int i = 0; i<tabs.getChildCount(); i++) {
            RelativeLayout tab = (RelativeLayout) tabs.getChildAt(i);
            tab.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.tabindicator));

The tabindicator drawable is as follows:

<?xml version="1.0" encoding="utf-8" ?> 
<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="@drawable/tab_unselected" /> 
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" /> 
<!--  Focused states  --> 
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" /> 
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" /> 
 <!--  Pressed  --> 
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_press" /> 
<item android:state_pressed="true" android:drawable="@drawable/tab_press" /> 
</selector>

The drawables were just 9-patch images with the colour, although you may be able to get a similar effect using a standard colour.

like image 119
Stev_k Avatar answered Oct 19 '22 05:10

Stev_k