Error inflating ImageView / ImageButton with ColorStateList tint value
android, android-4.4-kitkat, android-appcompat, imageview, tint
Solution
In my case i replaced `android:tint` with `app:tint` and added to root element `xmlns:app="http://schemas.android.com/apk/res-auto"`. It fixed crashing issue on API level < 21.
And color state selector /res/color/color_selector.xml looks like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#151515"/>
<item android:state_focused="true" android:color="#151515"/>
<item android:color="#424242"/>
</selector>
Problem
Using an `ImageView` / `ImageButton` (`AppCompatImageView` / `AppCompatImageButton`) in conjunction with a style attribute of `android:tint` which makes use of a `ColorStateList` resource works fine on >= API 21, but throws an `InflateException` on API < 21. Firstly, I don't even know whether the `AppCompatImageView / (Button)` tinting supports `ColourStateList` xml resources as an `android:tint` value, I can't seem to find a definitive answer to this. Suggestions I can find on S/O suggest implementing a `TintableImageView` etc, but these answers are quite dated, and it seems from the source of the appcompat implementations this should be a feature. To clarify this is definitely the issue. Removing the `android:tint` attribute or setting it to a single colour resource works. Also to clarify, I'm aware this is achievable programmatically. I'm trying to get it backwards compatible in xml. Minimal example activity_foo.xml ``` <android.support.v7.widget.AppCompatImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_caret_up" style="@style/IconButton.Primary" /> ``` styles.xml ``` <style name="IconButton.Primary"> <item name="android:tint">@color/link_button_color</item> </style> ``` link_button_color.xml ``` <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/btnLinkPressedTextColor" android:state_selected="true" /> <item android:color="@color/btnLinkPressedTextColor" android:state_pressed="true" /> <item android:color="@color/btnLinkTextColor" /> </selector> ```