How to change Tab Indicator color programmatically
Solution 1
I use Jeff Gilfelt,s Android Action Bar Style Generator. You can use GUI to style your tabs and at the end you get the source code which you can use, review and modify accordingly. :)
Here's a link.
Solution 2
You can implement by create a custom tab indicator view and use setIndicator to implement different different indicator for tabs.
Solution 3
The one and the best way to change selector color is to use Styles (I saw "Please Note", btw).
In drawable
folder create tab_selector.xml
and do something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/tab_selected_pink" />
<item android:state_selected="false" android:drawable="@drawable/tab_unselected_white" />
</selector>
and then in your values/styles.xml
I guess, do something like this:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:actionBarTabBarStyle">@style/MyTabStyle</item>
</style>
<style name="MyTabStyle" parent="android:Widget.ActionBar.TabBar">
<item name="android:background">@drawable/tab_indicator_selector</item>
</style>
I'm possibly wrong with item
name attribute
in first style and parent attribute
in second style. But in common it's will look like this.
As you can see it is easy to do. All that you really have to do is make 9patch drawables, if you want to support different screens.
Also you can look at Jake Wharton's ViewPagerIndicator that's is most flexible way to use any Navigation Mode.
Related videos on Youtube
Comments
-
JESUS over 1 year
I am new to Android, and before starting programming i found that now a days many of the apps are using Fragments, especially Tab with Swipeable Views
How to change Tab Indicator/highlight color (I googled and changed ActionBar color to RED programmatically), but don't know how to change Tab Indicator color to RED. (priority programmatically)
still my ActionBar looks like this
I am using below lines to change background color of ActionBar, but i also need to change the color of Tab Indicator programmatically.
actionBar = getActionBar(); actionBar.setBackgroundDrawable(new ColorDrawable(Color.RED));
-
Jade over 8 yearsThis question and answer relates to a deprecated tab implementation. Tabs should new be implemented using TabLayout from the Design support library. The indicator color can be changed with the
tabIndicatorColor
style attribute and the height can be changed with thetabIndicatorHeight
attribute.
-
-
JESUS about 10 yearsi know setIcon require to use drawable, but don't know how can i use different-different for each and every tabs