I'm using the TabLayout for my menu with Icon buttons. Is it possible to tint the icons via XML in drawables?
android:tint doesn't work with a TabItem element.
If you want to set tint through XML there is one way. Is custom layout for TabItem
which is set through attribute android:layout
:
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_drawable"
android:layout="@layout/custom_tab" />
Where custom_tab
layout is:
<?xml version="1.0" encoding="utf-8"?>
<com.view.TintableImageView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:tint_color="@color/red" />
Source of TintableImageView
you can find here.
This approach gives you flexibility if you want to use different icon colors for different selector states, just create color selector file and set it as a tint color and it works
app:tint_color="@color/selector_tab"
I have found a much simpler solution, in the layout file add this attribute to the TabLayout:
app:tabIconTint="@color/desired_color_or_selector"
if you want to preserve the color state (enable/disable/selected) give it a color selector like so:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_selected="true" />
<item android:color="@color/colorPrimary" android:state_focused="true" />
<item android:color="@color/enabled_color" android:state_enabled="true" />
<item android:color="@color/disabled_color" />
</selector>
You can do it coding part, try this