I want to have all tabs in ActionBar to have different Color Indicators, for example blue for tab 1, red for tab 2 etc.
To achieve that I did create different selectors for all colors and put them in different xmls in drawable. In style.xml I am calling them by
<style name="MyTheme" parent="AppBaseTheme">
<item name="android:actionBarTabStyle">@style/ActionBarTabStyleRed</item>
</style>
<style name="ActionBarTabStyleRed">
<item name="android:background">@drawable/tab_indicator_red</item>
</style>
I've created that style for different colors too. Now when I change the drawable or style to different color, it works. I can see colors being applied to tabs. But since all tabs are of same color, it didn't solve my purpose. I tried to set style of tabs in onTabSelected()
but there's no method to do that.
Ultimately I tried to create different themes for different colors and tried to set them programtically in onTabSelected()
, but then I got to know that theme must be set before setContentView()
.
So My questions is..How can I do that? Is there any way I can have different colors for different tab indicators???
Update:-
the drawable/tab_indicator_red.xml
<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_red" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_red" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_red" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_red" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_red" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_red" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_red" />
</selector>
I think I have a starting point for you but it is a little bit fiddly. Setting up the action bar with the following code you can set the tabs to use you custom look:
createTab method as follows:
However the actionbar seems to add the tab view to a parent view that adds padding so for each one you need to remove it. I did this in the oncreate after the first code block like so:
You may find a more graceful/straightforward way of doing this but as a starting point I thought it was worth posting.
Hope this helps.
ADDED:
It may also be worth pointing out that if you have a more complicated layout for your tab you can inflate a layout xml file in the createTab method instead. For example using the following xml (mytab.xml):
createTab becomes: