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:fillColor='Color HEX CODE'
are different), and then create a selector (selector_tabitem_list.xml) to change the drawable color when the state is changed.
// @drawable/selector_tabitem_list.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:drawable="@drawable/ic_list_selected_24dp"
android:state_selected="true" />
<item android:drawable="@drawable/ic_list_unselected_24dp"
android:state_selected="false" />
</selector>
It's kind of duplicated because two drawables are the same.
Selector cannot be used in vector drawable.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@drawable/selector"
android:pathData="M19,3...."
</vector>
--
// @drawable/selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<color android:color="@color/itemSelected" />
</item>
<item android:state_selected="false">
<color android:color="@color/itemUnselected" />
</item>
</selector>
, and android:fillColor="@color/state"
either.
// @color/state
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/white" android:state_selected="true" />
<item android:color="@android:color/black" android:state_selected="false" />
</selector>
Is there any way to use one drawable and change its color dynamically? Using hard hex code is better?
Thanks.
android:drawableTint
for the view – Discusandroid:tint="@color/tab_state"
in the vector tag. – Neo