1
tabLayout.AddTab(tabLayout.NewTab().SetIcon(Resource.Drawable.Home24),true);

I want :

default color of icon is #F3F3F3(unclicked)

default color of icon is #00000(clicked).

xleon
  • 6,201
  • 3
  • 36
  • 52
J. Joe
  • 383
  • 6
  • 26
  • Note the following answers - [David_E answer](http://stackoverflow.com/a/30904139/4059570) - [Tako answer](http://stackoverflow.com/a/33230289/4059570) – Andromer May 30 '16 at 07:52

1 Answers1

3

First, create a color selector in Resources/drawable/tab_icon_color.xml:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#000000" android:state_selected="true" />
    <item android:color="#F3F3F3" />
</selector>

To get a ColorStateList from that xml:

var tabIconColors = Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop
                        ? Resources.GetColorStateList(Resource.Drawable.home_tab_icon_color, Activity.Theme)
                        : Resources.GetColorStateList(Resource.Drawable.home_tab_icon_color); 

Then, for each tab:

var tab = tabLayout.NewTab().SetIcon(Resource.Drawable.Whatever);

// we wrap the icon to support API < 21
var iconWrap = DrawableCompat.Wrap(tab.Icon);
DrawableCompat.SetTintList(iconWrap, tabIconColors);
tab.SetIcon(iconWrap);

tabLayout.AddTab(tab);

Results (with different colors though):

enter image description here

xleon
  • 6,201
  • 3
  • 36
  • 52
  • 2
    should be marked as answer, works perfectly. The image example uses #FFFFFF for selected and the material guidelines 70% of that color, which is #B3B3B3. – ericosg Nov 30 '16 at 10:02