Tinting ImageView not working on Android 5.0. Ideas how to make it work again?
Asked Answered
J

4

17

In an application I've built I noticed that the ImageViews are not tinted on devices running the new Android Lollipop. This is the code that used to work correctly on older versions of the OS:

<ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="bottom|right"
            android:contentDescription="@string/descr_background_image"
            android:src="@drawable/circle_shape_white_color"
            android:tint="@color/intent_circle_green_grey" />

and this is the drawable that is loaded in the ImageView:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <gradient android:startColor="@color/white" android:endColor="@color/white"
        android:angle="270"/>
</shape>

Once again, this is working correctly on devices running JellyBean/Kitkat, but the tint has no effect on devices running Lollipop. Any ideas how to fix it? Is it a bug in the OS, or should I start tinting the image differently?

Judaism answered 27/11, 2014 at 16:14 Comment(2)
OS bug that's been fixed for a future release. ImageView's tint attribute was updated to use Drawable.setTint() rather than Drawable.setColorFilter(), but GradientDrawable didn't support setTint(). There is not really a good workaround for this from XML, but you can call setColorFilter() from code.Ahwaz
Awesome, it's good to know that you've already taken care of it! Thanks for the answer :)Judaism
I
9

As per @alanv comment, here goes the hacky fix to this bug. Basic idea is to extend ImageView and apply ColorFilter right after inflation:

public class TintImageView extends ImageView {

    public TintImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        initView();
    }

    private void initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ColorStateList imageTintList = getImageTintList();
            if (imageTintList == null) {
                return;
            }

            setColorFilter(imageTintList.getDefaultColor(), PorterDuff.Mode.SRC_IN);
        }
    }
}

As you might guess, this example is somewhat limited (Drawable set after inflation tint won't be updated, only default color of ColorStateList is used, and maybe something else), but if you got the idea you can fit it to your use-case.

Interstate answered 18/1, 2015 at 18:0 Comment(0)
D
20

Use the AppCompatImageView like so:

<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"
        android:tint="#636363"
    />

Make sure you have the latest compile 'com.android.support:appcompat-v7:23.4.0' in your app's build.gradle.

Durston answered 20/10, 2016 at 7:43 Comment(0)
I
9

As per @alanv comment, here goes the hacky fix to this bug. Basic idea is to extend ImageView and apply ColorFilter right after inflation:

public class TintImageView extends ImageView {

    public TintImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        initView();
    }

    private void initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ColorStateList imageTintList = getImageTintList();
            if (imageTintList == null) {
                return;
            }

            setColorFilter(imageTintList.getDefaultColor(), PorterDuff.Mode.SRC_IN);
        }
    }
}

As you might guess, this example is somewhat limited (Drawable set after inflation tint won't be updated, only default color of ColorStateList is used, and maybe something else), but if you got the idea you can fit it to your use-case.

Interstate answered 18/1, 2015 at 18:0 Comment(0)
D
1

this code work for me in android lollipop

ImageViewCompat.setImageTintList(imageView,ColorStateList.valueOf(Color.parseColor(chartTable.getReport().getButtonColor())));
Donetsk answered 27/5, 2020 at 11:59 Comment(0)
E
1

sometimes in xml drawable coded color and alpha number. you need change or remove this. like this:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24"
    android:tint="#FFFFFF"
    android:alpha="0.8">
  <path
      android:fillColor="@android:color/white"
      android:pathData="M16,1L8,1C6.34,1 5,2.34 5,4v16c0,1.66 1.34,3 3,3h8c1.66,0 3,-1.34 3,-3L19,4c0,-1.66 -1.34,-3 -3,-3zM14,21h-4v-1h4v1zM17.25,18L6.75,18L6.75,4h10.5v14z"/>
</vector>

to:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24"
    android:tint="@android:color/white">
  <path
      android:fillColor="@android:color/white"
      android:pathData="M16,1L8,1C6.34,1 5,2.34 5,4v16c0,1.66 1.34,3 3,3h8c1.66,0 3,-1.34 3,-3L19,4c0,-1.66 -1.34,-3 -3,-3zM14,21h-4v-1h4v1zM17.25,18L6.75,18L6.75,4h10.5v14z"/>
</vector>

just read the xml image and remove alpha or change color of tine. if you need.

Eskimoaleut answered 18/9, 2021 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.