ImageView tint below L cannot reference Theme Attribute

Viewed 829

I recently added different themes to my app and therefore need to have icons tinted differently for each theme.

<ImageView
        ...
        android:src="@drawable/ic_info"
        android:tint="?colorControlNormal"

But on API levels below 21 this does not work. I'm using vectordrawables and already tried to use

vectorDrawables.useSupportLibrary = true

and

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

but that didn't help either.

The tinting support documentation is quite ambiguous and with the combination of using VectorDrawables and referencing a theme color, I couldn't find any information.

currently trying to use:

<android.support.v7.widget.AppCompatImageView
    ...
    android:tint="?colorControlNormal"
    android:src="@drawable/icon"/>

which results in:

Caused by: android.view.InflateException: Binary XML file line #39: Error inflating class android.support.v7.widget.AppCompatImageView
   at android.view.LayoutInflater.createView(LayoutInflater.java:621)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
...
Caused by: java.lang.reflect.InvocationTargetException
   at java.lang.reflect.Constructor.constructNative(Native Method)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
...
Caused by: java.lang.NumberFormatException: Invalid int: "res/color/abc_secondary_text_material_light.xml"
   at java.lang.Integer.invalidInt(Integer.java:137)
   at java.lang.Integer.parse(Integer.java:374)
   at java.lang.Integer.parseInt(Integer.java:365)
   at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:122)
   at android.content.res.TypedArray.getInt(TypedArray.java:255)
   at android.widget.ImageView.<init>(ImageView.java:155)
   at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
   at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
2 Answers

Finally found the issue!

By referencing the theme color ?colorControlNormal

you implicitly use a ColorStateList (abc_secondary_text_material_light.xml) which is not supported below L. Unless you are using <android.support.v7.widget.AppCompatImageView> and also use the tint attribute from the support lib app:tint attribute. So the proper tint attribute solved it in the end.

Try using:

<android.support.v7.widget.AppCompatImageView
    android:id="@+id/my_appcompat_imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/my_image" // change to ur own.
    android:tint="#636363" // also change this part to our own case
/>

This work to API < 19

Related