1

How do you set the color of the tab host indicator color I want to change the light blue default color to lets says RED.

enter image description here

And I need to this programatically as I am making the tabs programatically.

I did some research and looked at this example but it does not work for me. With the progamtic approach.

TabWidget current tab bottom line color

Thanks

Community
  • 1
  • 1
iqueqiorio
  • 1,149
  • 2
  • 35
  • 78
  • Actually, the solution in the linked answer works, even if the tabs are added programmatically. Did you try it? Are you encountering any problems? – matiash Dec 23 '14 at 00:25
  • @matiash yes but I don't want the color to always be white – iqueqiorio Dec 23 '14 at 01:24
  • Just to clarify... do you need to change the color dynamically? As in, one time they're red and the next they're blue? That's also possible, but slightly more complicated. – matiash Dec 23 '14 at 01:40

1 Answers1

1

You can do this programmatically, even change the color as you want, by following the solution in the linked question you mention, plus adding a ColorFilter to adjust the color.

So:

  1. Create the appropriate drawable. The easiest way, as mentioned in one of the answers, is using https://jgilfelt.github.io/android-actionbarstylegenerator/
  2. Place into your project the tab_indicator_ab_example.xml (in drawable) plus the 6 associated png files (tab_*.png) for each drawable density.
  3. After creating the tabs, use the code that iterates over the TabWidget child views to set their background, however:
  4. Instead of setting the drawable as-is, use a color filter to change its color to the desired one.

Instead of this code:

for(int i = 0; i < widget.getChildCount(); i++) {
    ... /* same as before */

    v.setBackgroundResource(R.drawable.your_tab_selector_drawable);
}

write something like this:

for(int i = 0; i < widget.getChildCount(); i++) {
    ... /* same as before */

    Drawable d = getResources().getDrawable(R.drawable.tab_indicator_ab_example);
    d.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN);
    v.setBackgroundDrawable(d);
}
matiash
  • 54,791
  • 16
  • 125
  • 154
  • yes one time when it is open I want it lets say GREEN and another time RED. I add the xml file to my drawable correct? – iqueqiorio Dec 23 '14 at 02:43
  • @iqueqiorio From the zip that the style generator creates, copy those I mentioned into your project's res. – matiash Dec 23 '14 at 03:34
  • Okay got it :) the only thing is it says that `setBackgroundDrawable` is deprecated is there a way around this, I just don't like using deprecated methods – iqueqiorio Dec 23 '14 at 06:42
  • @iqueqiorio you can use setbackground in jelly bean or later and the old method for previous versions. – matiash Dec 23 '14 at 12:00