Tab Icons: My current method is to create two files (ic_list_selected_24dp.xml and ic_list_unselected_24dp.xml; they are basically the same but only the android:fillCo
Here is the complete list of steps to use a vector asset as tinted icon in a TabItem (which is part of the support design lib). All parts are present in the original question and linked answer, but are easy to miss.
state_selected
(as included in the question, but not in the answer linked by @cmingmai. There it only states android:state_enabled which is not relevant for tabs):res/color/selector_navigation.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:color="@android:color/black" />
<item android:state_selected="true" android:color="@android:color/white" />
</selector>
android:tintMode
and android:tint
to the vector tag.
In addition for the tinting to work with multiply, the fillColor of the path needs to be set to white!res/drawable/ic_tab_list:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0"
android:tintMode="multiply"
android:tint="@color/selector_navigation">
<path
android:fillColor="@android:color/white"
android:pathData="..." />
</vector>
Relevant part of the layout:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@android:color/white">
<android.support.design.widget.TabItem
android:id="@+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_tab_list" />
<!-- More Tabs -->
</android.support.design.widget.TabLayout>
Adjust build.gradle with following to activate vector support for old Android versions (in case it was not already previously added):
android { defaultConfig { vectorDrawables.useSupportLibrary = true } }
Try replacing the AppTheme in style.xml with a different parent like so:
style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"
Upgrade to Support Library 28.0.0
+ (presumably same for androidx
.. didn't test).
On Support Library 28.0.0
using a selector color xml resource seems to work now when used as vector's android:fillColor
.
In fact the previous solution (using vector's android:tintMode
, android:tint
and white for android:fillColor
) no longer works for me on Support Library 28.0.0
.
Tested on API 21 and 27 Emulators.